- 浏览: 409665 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
dageda_jgw:
...
JSF定义及原理 -
尘土飞扬:
这个在NB7中已经无效了,加不加都会提示 UTF8打不开
Netbeans统一UTF-8编码的方法 -
ouyangkang20:
不对
Struts target is null for setProperty(null, "x", [Ljava.lang.String;@179da9f) -
wpf523:
hahah,很不错啊
mysql安装图解教程 5.1 -
taburissmeng:
action中相应的属性没有set get方法就会报这样的错。 ...
Struts target is null for setProperty(null, "x", [Ljava.lang.String;@179da9f)
、最简单的SSH程序
概要
本文将完成最常见的login功能。
1.最简单的Struts程序
2.集成Spring和Hibernate
3.数据访问层采用JPA
1.最简单的Struts程序
说它是Struts程序是因为没有Struts运行不了,说它最简单是因为仅仅执行了一个转发。
1)配置Struts
1.1)在web.xml文件中加入以下配置:
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
添加后的完整web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="bitrac" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Bitrac Blog</display-name>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.2)在Bitrac\src下新建struts.xml文件(注:不带盘符的指在Eclipse中操作,以后不再说明),内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.action.extension" value="jspx" />
<package name="default" extends="struts-default">
<action name="admin">
<result>/admin/index.jsp</result>
</action>
</package>
</struts>
2)新建/admin/index.jsp文件(这样的形式指在Bitrac\WebRoot新建目录admin和文件index.jsp)内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Struts Test</title>
</head>
<body>
<p>Hello From Struts</p>
</body>
</html>
修改/index.jsp,内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="0;url=admin.jspx" />
</head>
<body>
<p>Loading ...</p>
</body>
</html>
3) 运行
点击eclipse工具条上的按钮启动Tomcat,
浏览http://localhost:8080/bitrac/,正常的话应该会看到“Hello From Struts”。
2.集成Spring和Hibernate
1) Struts2和Spring2集成
1.1)将D:\JavaTools\struts-2.1.2\lib\下的
struts2-spring-plugin-2.1.2.jar复制到
D:\Project\Bitrac\WebRoot\WEB-INF\lib\
1.2)在web.xml文件中加入Spring配置:
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
加入后完整的文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="bitrac" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Bitrac Blog</display-name>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.3)新建Struts配置文件Bitrac\src\default.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="admincp">
<result>/admin/login.jsp</result>
</action>
</package>
</struts>
这个配置文件将用来存放前台的action配置
1.4)新建Struts配置文件Bitrac\src\admin.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="admin" extends="struts-default" namespace="/admin">
<action name="login" class="adminAction" method="login">
<result name="success">/admin/index.jsp</result>
<result name="input">/admin/login.jsp</result>
</action>
</package>
</struts>
这个文件将用来存放后台管理界面的Action配置
1.5)修改Bitrac\src\struts.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.action.extension" value="jspx" />
<include file="default.xml" />
<include file="admin.xml" />
</struts>
这里配置了一些全局常量,struts.action.extentsion是访问action时的后缀,通常用的有action或者do,我的习惯是用jspx,例如/admin/login.jspx,将调用admin.xml里定义的login action。
1.6)新建Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" />
</beans>
注意这里的bean name是adminAction,再看Bitrac\src\admin.xml里的class="adminAction",Spring和Struts的结合点就在这里,后台处理这一结合的是struts2-spring-plugin-2.1.2.jar。如果不使用spring,在admin.xml中将是class="com.albertsong.bitrac.action.AdminAction"。
1.7)新建AdminAction类
新建文件Bitrac\src\com\albertsong\bitrac\action\AdminAction.java,(以后所有XXAction都放在相同的目录下,不再重复说明)内容如下:
package com.albertsong.bitrac.action;
import com.albertsong.bitrac.dao.Member;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class AdminAction {
private Log log = LogFactory.getLog(AdminAction.class);
private Member member;
public Member getMember() {
return member;
}
public void setMember(Member member) {
this.member = member;
}
public String login(){
log.debug("member.username="+member.getUsername());
log.debug("member.password="+member.getPassword());
return "success";
}
}
要让log起作用需要将
D:\JavaTools\spring-framework-2.5.5\lib\log4j\log4j-1.2.15.jar
D:\JavaTools\spring-framework-2.5.5\lib\jakarta-commons\commons-logging.jar
复制到/WEB-INF/lib下
将commons-logging.jar改名为commons-logging-1.1.1.jar,并将commons-logging-api-1.1.jar删掉。(我们只需要同一个组件的最新版本,多余的将被删除,以后不再重复说明,如果大家发现加入到/WEB-INF/lib里的后来又没了,那就是删除了)。
然后在Eclipse里点选Bitrac,按F5刷新,这时工程的文件列表已经更新。Alt+Enter打开工程属性对话框。Java Build PathLibrariesAdd JARs…将commons-logging-1.1.1.jar添加到编译路径,log4j-1.2.15.jar不需要添加,这个是在运行时起作用,编译时不需要。添加好以后,编译的错误提示应该都消失了。
做好这些以后还需要对Log4j做一些配置。
在/WEB-INF/web.xml文件中添加
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>bitrac.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
bitrac.root定义了一个代表Web应用程序根目录的变量,这个变量会在log4j.properties中使用。
加好以后完整的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="bitrac" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Bitrac Blog</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>bitrac.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
新建Bitrac\src\log4j.properties文件,内容如下:
log4j.rootLogger=INFO, stdout, logfile
log4j.logger.com.albertsong=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${bitrac.root}/WEB-INF/log/bitrac.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
新建目录/WEB-INF/log
新建com.albertsong.bitrac.dao.Member.Java,内容如下:
package com.albertsong.bitrac.dao;
import java.util.Date;
/**
* @author Albert
*
*/
public class Member {
private int id;
private short grade;
private String username;
private String password;
private String validate;
private boolean conserve;
private String contact;
private String picture;
private String explain;
private int numPost;
private int numComm;
private int numFile;
private Date publish;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public short getGrade() {
return grade;
}
public void setGrade(short grade) {
this.grade = grade;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getValidate() {
return validate;
}
public void setValidate(String validate) {
this.validate = validate;
}
public boolean isConserve() {
return conserve;
}
public void setConserve(boolean conserve) {
this.conserve = conserve;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getExplain() {
return explain;
}
public void setExplain(String explain) {
this.explain = explain;
}
public int getNumPost() {
return numPost;
}
public void setNumPost(int numPost) {
this.numPost = numPost;
}
public int getNumComm() {
return numComm;
}
public void setNumComm(int numComm) {
this.numComm = numComm;
}
public int getNumFile() {
return numFile;
}
public void setNumFile(int numFile) {
this.numFile = numFile;
}
public Date getPublish() {
return publish;
}
public void setPublish(Date publish) {
this.publish = publish;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
这里将Member.java放在dao下面,文件如何组织,包括类如何设计,完全是人为的,所谓的惯例和标准只是大家都公认的一些比较好的做法而已。我认为,目前将Member.java放在这里就可以了。
1.8)修改/index.jsp,内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="0;url=admincp.jspx" />
</head>
<body>
<p>Loading ...</p>
</body>
</html>
1.9)新建文件/admin/login.jsp,内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Bitrac Demo Site - 管理员控制面板</title>
</head>
<body>
<div id="content">
<div id="AdmEnter">
<s:actionerror />
<s:form action="login" namespace="/admin">
<fieldset><legend>管理登录</legend>
<s:textfield label="用户名" name="member.username" />
<s:password label="密码" name="member.password" />
<s:submit value="登录" />
</fieldset>
</s:form>
</div>
</div>
<div id="footer">
<p>Powered by <a href="http://www.albertsong.com" target="_blank">Bitrac(J) v0.1</a>. Designed by <a href="mailto:loveyuki@gmail.com">Loveyuki</a> &Programmed by <a href="mailto:albertsong.com@gmail.com">Albert Song</a></p>
</div>
</body>
</html>
这里使用了struts的标签来将视图层(JSP)和控制层(Action)关联在一起。注意到<s:form action="login" namespace="/admin">中的action和namespace就是我们在admin.xml中定义的。通过name="member.username"这样的形式就可以将Action中的member成员和界面元素关联起来。
1.10)启动Tomcat,浏览http://localhost:8080/bitrac/
如果上面的操作步骤都正确,那么会看到一个登录页面,点登录按钮会转到另一个页面。现在Struts2和Spring2已经集成在一起了。不过login里面我们还什么都没写,这个程序还没有任何有用的功能。如果在用户名和密码框中填入内容,点登录以后在Eclipse的Console里会看到相应的调试信息。
这里有很多警告,原因不清楚,不想看到它们并不难,在log4j.properties文件中log4j.logger.com.albertsong=DEBUG的下面加一行
log4j.logger.com.opensymphony.xwork2=ERROR
好了,现在清静了。
2)Spring2和Hibernate集成
2.1)复制必要的jar
将
D:\JavaTools\spring-framework-2.5.5\lib\j2ee\jta.jar
D:\JavaTools\spring-framework-2.5.5\lib\c3p0\c3p0-0.9.1.2.jar
D:\JavaTools\hibernate-3.2\lib\antlr-2.7.6.jar
D:\JavaTools\hibernate-3.2\lib\asm.jar
D:\JavaTools\hibernate-3.2\lib\asm-attrs.jar
D:\JavaTools\hibernate-3.2\lib\cglib-2.1.3.jar
D:\JavaTools\hibernate-3.2\lib\commons-collections-2.1.1.jar
D:\JavaTools\hibernate-3.2\lib\dom4j-1.6.1.jar
D:\JavaTools\hibernate-3.2\lib\ehcache-1.2.3.jar
D:\JavaTools\hibernate-3.2\lib\jaxen-1.1-beta-7.jar
复制到/WEB-INF/lib下
2.2)修改配置文件
在/WEB-INF/applicationContext.xml中加入Hibernate相关配置。
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bitrac" />
<property name="user" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/albertsong/bitrac/dao/Member.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
加入数据访问类的配置
<bean id="memberDao" class="com.albertsong.bitrac.dao.MemberDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
给<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" />
添加属性<property name="memberDao" ref="memberDao" />
完成以后完整的applicationContext.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bitrac" />
<property name="user" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/albertsong/bitrac/dao/Member.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
<bean id="memberDao" class="com.albertsong.bitrac.dao.MemberDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" scope="prototype">
<property name="memberDao" ref="memberDao" />
</bean>
</beans>
(要注意的是如果没有scope="prototype",则登录出错以后,不能再正确登录,原因显然是AdminAction缓存了member的数据,具体原理暂不研究)
新建Bitrac\src\com\albertsong\bitrac\dao\Member.hbm.xml,这是Hibernate的数据映射文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.albertsong.bitrac.dao">
<class name="Member" table="BT_Member">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="grade" />
<property name="username" />
<property name="password" />
<property name="validate" />
<property name="conserve" />
<property name="contact" />
<property name="picture" />
<property name="explain" />
<property name="numPost" />
<property name="numComm" />
<property name="numFile" />
<property name="publish" />
<property name="address" />
</class>
</hibernate-mapping>
这里使用了package,如果不使用package,则class的name要使用com.albertsong.bitrac.dao.Member这样的。property的name和Member的属性对应,因为表BT_Member的列名和Member的属性一样,所以不需要使用<column />标签。
2.3)新建DAO类文件
新建MemberDao.java,内容如下:
package com.albertsong.bitrac.dao;
public interface MemberDao {
boolean exist(String username,String password);
}
新建MemberDaoImpl.Java内容如下:
package com.albertsong.bitrac.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao {
@SuppressWarnings("unchecked")
@Override
public boolean exist(String username,String password) {
List ls=getHibernateTemplate().find("from Member m where m.username=? and m.password=MD5(?)",
new Object[]{username,password});
return ls.size()==1?true:false;
}
}
修改AdminAction.java,内容如下:
package com.albertsong.bitrac.action;
import com.albertsong.bitrac.dao.Member;
import com.albertsong.bitrac.dao.MemberDao;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@SuppressWarnings("serial")
public class AdminAction extends ActionSupport {
private Log log = LogFactory.getLog(AdminAction.class);
private Member member;
private MemberDao memberDao;
public Member getMember() {
return member;
}
public void setMember(Member member) {
this.member = member;
}
public MemberDao getMemberDao() {
return memberDao;
}
public void setMemberDao(MemberDao memberDao) {
this.memberDao = memberDao;
}
public String login(){
log.debug("member.username="+member.getUsername());
log.debug("member.password="+member.getPassword());
if(memberDao.exist(member.getUsername(), member.getPassword())){
return "success";
}else{
addActionError("username or passowrd is not correct.");
return "input";
}
}
}
3)转换和导入数据库
怎么转换的就不说了,这个和本教程没有关系,有兴趣的可以看看
http://www.loveyuki.com/Article/Bitrac_Alpha_Update_List.aspx
建目录D:\Project\Bitrac\doc,转换后的文件create_bitrac_db.sql放到这个目录下。(sql文件的内容这里不再重复,下载源代码包以后可以找到)。
先执行D:\mysql-5.0.51b\bin\mysqld-nt.exe启动mysql数据库。
运行D:\MySQL GUI Tools 5.0\MySQLQueryBrowser.exe,
File-->Open Script...,打开sql脚本文件,执行,创建bitrac数据库。
下载mysql的jdbc驱动MySQL Connector/J 5.1.6
下载页面:
http://dev.mysql.com/downloads/connector/j/5.1.html
下载链接:
http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.6.zip/from/http://ftp.iij.ad.jp/pub/db/mysql/
将下载的文件解压,将其中的mysql-connector-java-5.1.6-bin.jar复制到/WEB-INF/lib下。
4)运行程序
如果以上每一步都没有做错的话,现在我们已经有了一个简单的可以登录的程序了。
启动tomcat,浏览http://localhost:8080/bitrac/,输入用户名Admin,密码Admin888试试吧。
当然这个程序目前还相当简单,虽然写得已经很长了,但可能还是没有看到你想看的东西吧。我也突然觉得要写这个教程的工程相当庞大,随便一个组件拉出来都得写一本好几百页的书。怎样才能写得简单又清楚,确实是一个问题。但有一个目标是很明确的,那就是使用最简单的,最新的技术。上面讲的Hibernate使用了老式的配置方法及一般的dataSource访问方式,没有涉及到事务。下面将其改成基于Spring Schema配置的JPA访问方式。(以后一般不会提供两种不同的方法的比较,将直接采用我认为较好的方法,当然可能我写完了发现还有更好的,那么就会有两种甚至两种以上的方法)。
3. 数据访问层采用JPA
1) jar准备
Hibernate EntityManager
下载页面:
http://hibernate.org/30.html
下载链接:
http://sourceforge.net/project/showfiles.php?group_id=40712&package_id=156160
下载的文件hibernate-entitymanager-3.4.0.GA.zip放在D:\JavaTools下
解压后将
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\hibernate-entitymanager.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\javassist.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\ejb3-persistence.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-annotations.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-commons-annotations.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-core.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\ slf4j-api.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\test\slf4j-log4j12.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\test\commons-collections.jar
复制到/WEB-INF/lib
然后给各个文件改名,加上版本号,即javassist-3.4.jar
ejb3-persistence-1.0.2GA.jar
hibernate-annotations-3.4.0GA.jar
hibernate-commons-annotations-3.1.0GA.jar
hibernate-core-3.3.0SP1.jar
hibernate-entitymanager-3.4.0GA.jar
版本号可以在jar文件中的META-INF\MANIFEST.MF文件中找到。然后将同名的版本较低的jar删除,选中项目名称,按F5刷新,这时可能会有错误提示,因为旧的jar被删除了,这时需要重新添加jar,这一操作过程以后不再重复说明。
2)Class文件修改
2.1)在Package Explorer树形列表中选中Member.java文件,点右键,Refactor-->Move…-->Create Package…,包名com.albertsong.bitrac.model,Finish,OK。
通过以上步骤,我们已经使用Eclipse的重构功能移动了Member.java的位置。
2.2)在Java Build Path中加入ejb3-persistence-1.0.2GA.jar,编译使用了JPA的类时要用到。
2.3)修改后的Member.java
package com.albertsong.bitrac.model;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="BT_Member")
public class Member {
@Id
@GeneratedValue
private int id;
private short grade;
private String username;
private String password;
private String validate;
private boolean conserve;
private String contact;
private String picture;
private String explain;
private int numPost;
private int numComm;
private int numFile;
private Date publish;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public short getGrade() {
return grade;
}
public void setGrade(short grade) {
this.grade = grade;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getValidate() {
return validate;
}
public void setValidate(String validate) {
this.validate = validate;
}
public boolean isConserve() {
return conserve;
}
public void setConserve(boolean conserve) {
this.conserve = conserve;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getExplain() {
return explain;
}
public void setExplain(String explain) {
this.explain = explain;
}
public int getNumPost() {
return numPost;
}
public void setNumPost(int numPost) {
this.numPost = numPost;
}
public int getNumComm() {
return numComm;
}
public void setNumComm(int numComm) {
this.numComm = numComm;
}
public int getNumFile() {
return numFile;
}
public void setNumFile(int numFile) {
this.numFile = numFile;
}
public Date getPublish() {
return publish;
}
public void setPublish(Date publish) {
this.publish = publish;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.4)修改后的MemberDaoImpl.java
package com.albertsong.bitrac.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class MemberDaoImpl implements MemberDao {
private Log log = LogFactory.getLog(MemberDaoImpl.class);
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
private EntityManager getEntityManager(){
return em;
}
@SuppressWarnings("unchecked")
@Override
public boolean exist(String username,String password) {
StringBuffer buffer = new StringBuffer("select m from Member m ");
buffer.append(" where m.username='").append(username).append("'");
buffer.append(" and m.password=MD5('").append(password).append("')");
log.debug(buffer.toString());
Query query = getEntityManager().createQuery(buffer.toString());
List ls = query.getResultList();
return ls.size()==1?true:false;
}
}
3) 配置文件修改
3.1)删除Bitrac\src\com\albertsong\bitrac\dao\Member.hbm.xml
3.2)建目录Bitrac\src\META-INF,在这个目录下面新建一个文件persistence.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="punit">
</persistence-unit>
</persistence>
3.3)修改web.xml文件,添加
<filter>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.4)修改applicationContext.xml,修改后内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/bitrac" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="memberDao" class="com.albertsong.bitrac.dao.MemberDaoImpl" />
<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" scope="prototype">
<property name="memberDao" ref="memberDao" />
</bean>
</beans>
4)运行程序
启动Tomcat,浏览http://localhost:8080/bitrac/
正常的话,我们已经完成了一个最简单的Struts2+Spring2.5+JPA(Hibernate)程序了。
源代码这里下载:
Lib 源代码不带jar,这里包括了当前需要的所有jar,解压后放到/WebRoot/WEB-INF/lib下。
http://www.albertsong.com/download/Bitrac/Bitrac_Lib.rar
不使用JPA的工程
http://www.albertsong.com/download/Bitrac/Bitrac_Src_2_1.rar
使用JPA的工程
http://www.albertsong.com/download/Bitrac/Bitrac_Src_2_2.rar
参考链接:
在struts2中集成Spring
http://www.99inf.net/SoftwareDev/Java/55131.htm
登录出错以后不能再登录
http://www.iteye.com/topic/146932
C3P0的详细配置
http://blog.csdn.net/luhao/archive/2008/05/06/2399946.aspx
JPA批注参考
http://www.cnblogs.com/kentyshang/archive/2008/01/07/1029142.html
后记:
如果仔细的看完这两章,那么对于Java Web开发常用的操作和一般流程应该熟悉了。从第三章开始,将正式开始Bitrac博客程序的开发。
概要
本文将完成最常见的login功能。
1.最简单的Struts程序
2.集成Spring和Hibernate
3.数据访问层采用JPA
1.最简单的Struts程序
说它是Struts程序是因为没有Struts运行不了,说它最简单是因为仅仅执行了一个转发。
1)配置Struts
1.1)在web.xml文件中加入以下配置:
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
添加后的完整web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="bitrac" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Bitrac Blog</display-name>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.2)在Bitrac\src下新建struts.xml文件(注:不带盘符的指在Eclipse中操作,以后不再说明),内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.action.extension" value="jspx" />
<package name="default" extends="struts-default">
<action name="admin">
<result>/admin/index.jsp</result>
</action>
</package>
</struts>
2)新建/admin/index.jsp文件(这样的形式指在Bitrac\WebRoot新建目录admin和文件index.jsp)内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Struts Test</title>
</head>
<body>
<p>Hello From Struts</p>
</body>
</html>
修改/index.jsp,内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="0;url=admin.jspx" />
</head>
<body>
<p>Loading ...</p>
</body>
</html>
3) 运行
点击eclipse工具条上的按钮启动Tomcat,
浏览http://localhost:8080/bitrac/,正常的话应该会看到“Hello From Struts”。
2.集成Spring和Hibernate
1) Struts2和Spring2集成
1.1)将D:\JavaTools\struts-2.1.2\lib\下的
struts2-spring-plugin-2.1.2.jar复制到
D:\Project\Bitrac\WebRoot\WEB-INF\lib\
1.2)在web.xml文件中加入Spring配置:
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
加入后完整的文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="bitrac" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Bitrac Blog</display-name>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.3)新建Struts配置文件Bitrac\src\default.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="admincp">
<result>/admin/login.jsp</result>
</action>
</package>
</struts>
这个配置文件将用来存放前台的action配置
1.4)新建Struts配置文件Bitrac\src\admin.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="admin" extends="struts-default" namespace="/admin">
<action name="login" class="adminAction" method="login">
<result name="success">/admin/index.jsp</result>
<result name="input">/admin/login.jsp</result>
</action>
</package>
</struts>
这个文件将用来存放后台管理界面的Action配置
1.5)修改Bitrac\src\struts.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.action.extension" value="jspx" />
<include file="default.xml" />
<include file="admin.xml" />
</struts>
这里配置了一些全局常量,struts.action.extentsion是访问action时的后缀,通常用的有action或者do,我的习惯是用jspx,例如/admin/login.jspx,将调用admin.xml里定义的login action。
1.6)新建Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" />
</beans>
注意这里的bean name是adminAction,再看Bitrac\src\admin.xml里的class="adminAction",Spring和Struts的结合点就在这里,后台处理这一结合的是struts2-spring-plugin-2.1.2.jar。如果不使用spring,在admin.xml中将是class="com.albertsong.bitrac.action.AdminAction"。
1.7)新建AdminAction类
新建文件Bitrac\src\com\albertsong\bitrac\action\AdminAction.java,(以后所有XXAction都放在相同的目录下,不再重复说明)内容如下:
package com.albertsong.bitrac.action;
import com.albertsong.bitrac.dao.Member;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class AdminAction {
private Log log = LogFactory.getLog(AdminAction.class);
private Member member;
public Member getMember() {
return member;
}
public void setMember(Member member) {
this.member = member;
}
public String login(){
log.debug("member.username="+member.getUsername());
log.debug("member.password="+member.getPassword());
return "success";
}
}
要让log起作用需要将
D:\JavaTools\spring-framework-2.5.5\lib\log4j\log4j-1.2.15.jar
D:\JavaTools\spring-framework-2.5.5\lib\jakarta-commons\commons-logging.jar
复制到/WEB-INF/lib下
将commons-logging.jar改名为commons-logging-1.1.1.jar,并将commons-logging-api-1.1.jar删掉。(我们只需要同一个组件的最新版本,多余的将被删除,以后不再重复说明,如果大家发现加入到/WEB-INF/lib里的后来又没了,那就是删除了)。
然后在Eclipse里点选Bitrac,按F5刷新,这时工程的文件列表已经更新。Alt+Enter打开工程属性对话框。Java Build PathLibrariesAdd JARs…将commons-logging-1.1.1.jar添加到编译路径,log4j-1.2.15.jar不需要添加,这个是在运行时起作用,编译时不需要。添加好以后,编译的错误提示应该都消失了。
做好这些以后还需要对Log4j做一些配置。
在/WEB-INF/web.xml文件中添加
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>bitrac.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
bitrac.root定义了一个代表Web应用程序根目录的变量,这个变量会在log4j.properties中使用。
加好以后完整的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="bitrac" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Bitrac Blog</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>bitrac.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
新建Bitrac\src\log4j.properties文件,内容如下:
log4j.rootLogger=INFO, stdout, logfile
log4j.logger.com.albertsong=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${bitrac.root}/WEB-INF/log/bitrac.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
新建目录/WEB-INF/log
新建com.albertsong.bitrac.dao.Member.Java,内容如下:
package com.albertsong.bitrac.dao;
import java.util.Date;
/**
* @author Albert
*
*/
public class Member {
private int id;
private short grade;
private String username;
private String password;
private String validate;
private boolean conserve;
private String contact;
private String picture;
private String explain;
private int numPost;
private int numComm;
private int numFile;
private Date publish;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public short getGrade() {
return grade;
}
public void setGrade(short grade) {
this.grade = grade;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getValidate() {
return validate;
}
public void setValidate(String validate) {
this.validate = validate;
}
public boolean isConserve() {
return conserve;
}
public void setConserve(boolean conserve) {
this.conserve = conserve;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getExplain() {
return explain;
}
public void setExplain(String explain) {
this.explain = explain;
}
public int getNumPost() {
return numPost;
}
public void setNumPost(int numPost) {
this.numPost = numPost;
}
public int getNumComm() {
return numComm;
}
public void setNumComm(int numComm) {
this.numComm = numComm;
}
public int getNumFile() {
return numFile;
}
public void setNumFile(int numFile) {
this.numFile = numFile;
}
public Date getPublish() {
return publish;
}
public void setPublish(Date publish) {
this.publish = publish;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
这里将Member.java放在dao下面,文件如何组织,包括类如何设计,完全是人为的,所谓的惯例和标准只是大家都公认的一些比较好的做法而已。我认为,目前将Member.java放在这里就可以了。
1.8)修改/index.jsp,内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="0;url=admincp.jspx" />
</head>
<body>
<p>Loading ...</p>
</body>
</html>
1.9)新建文件/admin/login.jsp,内容如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Bitrac Demo Site - 管理员控制面板</title>
</head>
<body>
<div id="content">
<div id="AdmEnter">
<s:actionerror />
<s:form action="login" namespace="/admin">
<fieldset><legend>管理登录</legend>
<s:textfield label="用户名" name="member.username" />
<s:password label="密码" name="member.password" />
<s:submit value="登录" />
</fieldset>
</s:form>
</div>
</div>
<div id="footer">
<p>Powered by <a href="http://www.albertsong.com" target="_blank">Bitrac(J) v0.1</a>. Designed by <a href="mailto:loveyuki@gmail.com">Loveyuki</a> &Programmed by <a href="mailto:albertsong.com@gmail.com">Albert Song</a></p>
</div>
</body>
</html>
这里使用了struts的标签来将视图层(JSP)和控制层(Action)关联在一起。注意到<s:form action="login" namespace="/admin">中的action和namespace就是我们在admin.xml中定义的。通过name="member.username"这样的形式就可以将Action中的member成员和界面元素关联起来。
1.10)启动Tomcat,浏览http://localhost:8080/bitrac/
如果上面的操作步骤都正确,那么会看到一个登录页面,点登录按钮会转到另一个页面。现在Struts2和Spring2已经集成在一起了。不过login里面我们还什么都没写,这个程序还没有任何有用的功能。如果在用户名和密码框中填入内容,点登录以后在Eclipse的Console里会看到相应的调试信息。
这里有很多警告,原因不清楚,不想看到它们并不难,在log4j.properties文件中log4j.logger.com.albertsong=DEBUG的下面加一行
log4j.logger.com.opensymphony.xwork2=ERROR
好了,现在清静了。
2)Spring2和Hibernate集成
2.1)复制必要的jar
将
D:\JavaTools\spring-framework-2.5.5\lib\j2ee\jta.jar
D:\JavaTools\spring-framework-2.5.5\lib\c3p0\c3p0-0.9.1.2.jar
D:\JavaTools\hibernate-3.2\lib\antlr-2.7.6.jar
D:\JavaTools\hibernate-3.2\lib\asm.jar
D:\JavaTools\hibernate-3.2\lib\asm-attrs.jar
D:\JavaTools\hibernate-3.2\lib\cglib-2.1.3.jar
D:\JavaTools\hibernate-3.2\lib\commons-collections-2.1.1.jar
D:\JavaTools\hibernate-3.2\lib\dom4j-1.6.1.jar
D:\JavaTools\hibernate-3.2\lib\ehcache-1.2.3.jar
D:\JavaTools\hibernate-3.2\lib\jaxen-1.1-beta-7.jar
复制到/WEB-INF/lib下
2.2)修改配置文件
在/WEB-INF/applicationContext.xml中加入Hibernate相关配置。
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bitrac" />
<property name="user" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/albertsong/bitrac/dao/Member.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
加入数据访问类的配置
<bean id="memberDao" class="com.albertsong.bitrac.dao.MemberDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
给<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" />
添加属性<property name="memberDao" ref="memberDao" />
完成以后完整的applicationContext.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bitrac" />
<property name="user" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/albertsong/bitrac/dao/Member.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
<bean id="memberDao" class="com.albertsong.bitrac.dao.MemberDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" scope="prototype">
<property name="memberDao" ref="memberDao" />
</bean>
</beans>
(要注意的是如果没有scope="prototype",则登录出错以后,不能再正确登录,原因显然是AdminAction缓存了member的数据,具体原理暂不研究)
新建Bitrac\src\com\albertsong\bitrac\dao\Member.hbm.xml,这是Hibernate的数据映射文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.albertsong.bitrac.dao">
<class name="Member" table="BT_Member">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="grade" />
<property name="username" />
<property name="password" />
<property name="validate" />
<property name="conserve" />
<property name="contact" />
<property name="picture" />
<property name="explain" />
<property name="numPost" />
<property name="numComm" />
<property name="numFile" />
<property name="publish" />
<property name="address" />
</class>
</hibernate-mapping>
这里使用了package,如果不使用package,则class的name要使用com.albertsong.bitrac.dao.Member这样的。property的name和Member的属性对应,因为表BT_Member的列名和Member的属性一样,所以不需要使用<column />标签。
2.3)新建DAO类文件
新建MemberDao.java,内容如下:
package com.albertsong.bitrac.dao;
public interface MemberDao {
boolean exist(String username,String password);
}
新建MemberDaoImpl.Java内容如下:
package com.albertsong.bitrac.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao {
@SuppressWarnings("unchecked")
@Override
public boolean exist(String username,String password) {
List ls=getHibernateTemplate().find("from Member m where m.username=? and m.password=MD5(?)",
new Object[]{username,password});
return ls.size()==1?true:false;
}
}
修改AdminAction.java,内容如下:
package com.albertsong.bitrac.action;
import com.albertsong.bitrac.dao.Member;
import com.albertsong.bitrac.dao.MemberDao;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@SuppressWarnings("serial")
public class AdminAction extends ActionSupport {
private Log log = LogFactory.getLog(AdminAction.class);
private Member member;
private MemberDao memberDao;
public Member getMember() {
return member;
}
public void setMember(Member member) {
this.member = member;
}
public MemberDao getMemberDao() {
return memberDao;
}
public void setMemberDao(MemberDao memberDao) {
this.memberDao = memberDao;
}
public String login(){
log.debug("member.username="+member.getUsername());
log.debug("member.password="+member.getPassword());
if(memberDao.exist(member.getUsername(), member.getPassword())){
return "success";
}else{
addActionError("username or passowrd is not correct.");
return "input";
}
}
}
3)转换和导入数据库
怎么转换的就不说了,这个和本教程没有关系,有兴趣的可以看看
http://www.loveyuki.com/Article/Bitrac_Alpha_Update_List.aspx
建目录D:\Project\Bitrac\doc,转换后的文件create_bitrac_db.sql放到这个目录下。(sql文件的内容这里不再重复,下载源代码包以后可以找到)。
先执行D:\mysql-5.0.51b\bin\mysqld-nt.exe启动mysql数据库。
运行D:\MySQL GUI Tools 5.0\MySQLQueryBrowser.exe,
File-->Open Script...,打开sql脚本文件,执行,创建bitrac数据库。
下载mysql的jdbc驱动MySQL Connector/J 5.1.6
下载页面:
http://dev.mysql.com/downloads/connector/j/5.1.html
下载链接:
http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.6.zip/from/http://ftp.iij.ad.jp/pub/db/mysql/
将下载的文件解压,将其中的mysql-connector-java-5.1.6-bin.jar复制到/WEB-INF/lib下。
4)运行程序
如果以上每一步都没有做错的话,现在我们已经有了一个简单的可以登录的程序了。
启动tomcat,浏览http://localhost:8080/bitrac/,输入用户名Admin,密码Admin888试试吧。
当然这个程序目前还相当简单,虽然写得已经很长了,但可能还是没有看到你想看的东西吧。我也突然觉得要写这个教程的工程相当庞大,随便一个组件拉出来都得写一本好几百页的书。怎样才能写得简单又清楚,确实是一个问题。但有一个目标是很明确的,那就是使用最简单的,最新的技术。上面讲的Hibernate使用了老式的配置方法及一般的dataSource访问方式,没有涉及到事务。下面将其改成基于Spring Schema配置的JPA访问方式。(以后一般不会提供两种不同的方法的比较,将直接采用我认为较好的方法,当然可能我写完了发现还有更好的,那么就会有两种甚至两种以上的方法)。
3. 数据访问层采用JPA
1) jar准备
Hibernate EntityManager
下载页面:
http://hibernate.org/30.html
下载链接:
http://sourceforge.net/project/showfiles.php?group_id=40712&package_id=156160
下载的文件hibernate-entitymanager-3.4.0.GA.zip放在D:\JavaTools下
解压后将
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\hibernate-entitymanager.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\javassist.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\ejb3-persistence.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-annotations.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-commons-annotations.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\hibernate-core.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\ slf4j-api.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\test\slf4j-log4j12.jar
D:\JavaTools\hibernate-entitymanager-3.4.0.GA\lib\test\commons-collections.jar
复制到/WEB-INF/lib
然后给各个文件改名,加上版本号,即javassist-3.4.jar
ejb3-persistence-1.0.2GA.jar
hibernate-annotations-3.4.0GA.jar
hibernate-commons-annotations-3.1.0GA.jar
hibernate-core-3.3.0SP1.jar
hibernate-entitymanager-3.4.0GA.jar
版本号可以在jar文件中的META-INF\MANIFEST.MF文件中找到。然后将同名的版本较低的jar删除,选中项目名称,按F5刷新,这时可能会有错误提示,因为旧的jar被删除了,这时需要重新添加jar,这一操作过程以后不再重复说明。
2)Class文件修改
2.1)在Package Explorer树形列表中选中Member.java文件,点右键,Refactor-->Move…-->Create Package…,包名com.albertsong.bitrac.model,Finish,OK。
通过以上步骤,我们已经使用Eclipse的重构功能移动了Member.java的位置。
2.2)在Java Build Path中加入ejb3-persistence-1.0.2GA.jar,编译使用了JPA的类时要用到。
2.3)修改后的Member.java
package com.albertsong.bitrac.model;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="BT_Member")
public class Member {
@Id
@GeneratedValue
private int id;
private short grade;
private String username;
private String password;
private String validate;
private boolean conserve;
private String contact;
private String picture;
private String explain;
private int numPost;
private int numComm;
private int numFile;
private Date publish;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public short getGrade() {
return grade;
}
public void setGrade(short grade) {
this.grade = grade;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getValidate() {
return validate;
}
public void setValidate(String validate) {
this.validate = validate;
}
public boolean isConserve() {
return conserve;
}
public void setConserve(boolean conserve) {
this.conserve = conserve;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getExplain() {
return explain;
}
public void setExplain(String explain) {
this.explain = explain;
}
public int getNumPost() {
return numPost;
}
public void setNumPost(int numPost) {
this.numPost = numPost;
}
public int getNumComm() {
return numComm;
}
public void setNumComm(int numComm) {
this.numComm = numComm;
}
public int getNumFile() {
return numFile;
}
public void setNumFile(int numFile) {
this.numFile = numFile;
}
public Date getPublish() {
return publish;
}
public void setPublish(Date publish) {
this.publish = publish;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.4)修改后的MemberDaoImpl.java
package com.albertsong.bitrac.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class MemberDaoImpl implements MemberDao {
private Log log = LogFactory.getLog(MemberDaoImpl.class);
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
private EntityManager getEntityManager(){
return em;
}
@SuppressWarnings("unchecked")
@Override
public boolean exist(String username,String password) {
StringBuffer buffer = new StringBuffer("select m from Member m ");
buffer.append(" where m.username='").append(username).append("'");
buffer.append(" and m.password=MD5('").append(password).append("')");
log.debug(buffer.toString());
Query query = getEntityManager().createQuery(buffer.toString());
List ls = query.getResultList();
return ls.size()==1?true:false;
}
}
3) 配置文件修改
3.1)删除Bitrac\src\com\albertsong\bitrac\dao\Member.hbm.xml
3.2)建目录Bitrac\src\META-INF,在这个目录下面新建一个文件persistence.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="punit">
</persistence-unit>
</persistence>
3.3)修改web.xml文件,添加
<filter>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.4)修改applicationContext.xml,修改后内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/bitrac" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="memberDao" class="com.albertsong.bitrac.dao.MemberDaoImpl" />
<bean name="adminAction" class="com.albertsong.bitrac.action.AdminAction" scope="prototype">
<property name="memberDao" ref="memberDao" />
</bean>
</beans>
4)运行程序
启动Tomcat,浏览http://localhost:8080/bitrac/
正常的话,我们已经完成了一个最简单的Struts2+Spring2.5+JPA(Hibernate)程序了。
源代码这里下载:
Lib 源代码不带jar,这里包括了当前需要的所有jar,解压后放到/WebRoot/WEB-INF/lib下。
http://www.albertsong.com/download/Bitrac/Bitrac_Lib.rar
不使用JPA的工程
http://www.albertsong.com/download/Bitrac/Bitrac_Src_2_1.rar
使用JPA的工程
http://www.albertsong.com/download/Bitrac/Bitrac_Src_2_2.rar
参考链接:
在struts2中集成Spring
http://www.99inf.net/SoftwareDev/Java/55131.htm
登录出错以后不能再登录
http://www.iteye.com/topic/146932
C3P0的详细配置
http://blog.csdn.net/luhao/archive/2008/05/06/2399946.aspx
JPA批注参考
http://www.cnblogs.com/kentyshang/archive/2008/01/07/1029142.html
后记:
如果仔细的看完这两章,那么对于Java Web开发常用的操作和一般流程应该熟悉了。从第三章开始,将正式开始Bitrac博客程序的开发。
发表评论
-
target is null for setProperty(null, "x", [Ljava.lang.String;@b0c40e)错误异常
2012-05-22 18:06 1127在提交form表单的时候发现后台经常出现类似异常, ... -
Spring 表达式语言 (Spring Expression Language) SpEL
2012-03-15 11:21 2049Spring 3.0 创建了一种新的方式用以配置对象的注入(s ... -
freemarker数据格式化问题(即数值超过四位数时有逗号)
2009-10-17 22:34 4073当数据超过3位的时候,freemarker会自动用逗号截取 格 ... -
Struts target is null for setProperty(null, "x", [Ljava.lang.String;@179da9f)
2009-10-15 18:34 15361上午工作中出现了这样一个异常: ognl. ... -
freemarker 的内建函数 contains 的使用
2009-09-16 20:01 19940freemarker 的内建函数 contains 的使用:& ... -
FreeMarker学习
2009-09-16 19:29 1975关于FreeMarker 的基础学习请参考文档。。这里主要 ... -
JPA多对多的查寻方法
2009-09-03 12:12 3583第一种方法: 比如有2个entity class A { ... -
spring/struts2 整合(转载)
2009-07-31 16:00 916在Struts2中整合Spring的IoC ... -
Java开发,重在整合-----Struts 2整合Spring
2009-07-31 15:55 184313.2 Struts 2整合Spring Str ... -
Struts2.1.6与spring2.5.5集成
2009-07-29 17:00 1500把struts2-spring-plugin-2.1.6.ja ... -
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (3)
2009-07-29 16:46 1610进入我们基于Annotation的Struts2.0+H ... -
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (2)
2009-07-29 16:42 1389基于Annotation的SSH整合开发,其实,并没有我 ... -
基于Annotation的Struts2.0+Hibernate3.3+Spring2.5整合开发 (1)
2009-07-29 16:37 1309写这个主题的文章在 ... -
Struts2.1.6+Spring2.5.6+Hibernate3.3.1框架 整合开发
2009-07-29 16:29 2023来自:http://www.cnitblog.com/intr ... -
Struts2.1.6测试小记
2009-07-29 11:53 951Struts2.1.x终于推出正式版了,迫不急待地下了试试(读 ... -
集成struts2 spring hibernate,使用注解
2009-07-29 01:03 2128转载自:http://www.iteye.com/topic/ ...
相关推荐
标题"Struts2.X+Hibernate3.X+Spring2.5 整合"指的是将这三个框架的特定版本(Struts2.X,Hibernate3.X,Spring2.5)集成到同一个项目中,实现高效的企业级应用开发。这种整合能够充分利用各个框架的优势,使开发者...
Struts1.2、Spring2.5和Hibernate3.2是经典的Java企业级开发框架组合,它们各自在应用程序的不同层次上发挥着重要作用。Struts1.2是一个MVC(Model-View-Controller)框架,主要负责处理用户界面与业务逻辑之间的...
Struts2.1、Spring 2.5 和 Hibernate 3.2 是经典的Java Web开发框架组合,用于构建高效、可维护的企业级应用。这个详细例子将深入探讨如何使用这三个框架协同工作,实现数据库的增(Add)、删(Delete)、改(Modify...
总的来说,这个"struts2+spring2.5+hibernate3.2整合完整项目"提供了一个完整的开发实例,涵盖了前端到后端,以及数据库的各个环节。对于学习者而言,可以深入理解这三大框架的协同工作,提升Java Web开发技能。而...
myeclipse整合Struts1.2+Hibernate3.2+Spring2.5的留言板demo,可以参考参考,希望对你能有帮助,其中lib下的jar包对于ssh工程可以直接使用,原文地址: ...
Struts2.1、Spring2.5和Hibernate3.3是经典的Java企业级开发框架组合,它们各自在应用程序的不同层面提供了强大的支持。本篇将详细阐述这三个组件的整合过程,以及各自的核心特性。 首先,Struts2是一个基于MVC设计...
本文将详细介绍如何在Windows XP环境下,使用MyEclipse 7.0和Tomcat 6.0容器,配置一个基于Hibernate 3.2、Spring 2.5和Struts2.1的项目。 首先,创建一个新的Web工程,命名为"ssh",选择Java EE 5.0规范。接着,...
Struts2.1、Hibernate3.2和Spring2.5是Java Web开发中经典的MVC框架集成,被誉为“S2SH”组合。这个整合视频教程旨在帮助开发者掌握这三大框架的协同工作,提升Web应用的开发效率和质量。 Struts2作为Action层框架...
1. **整合目标**:本文档的目标是将 Struts2.x、Spring3.x、Hibernate4.x 与 DWR、jquery2.x 和 easyUI1.3.x 进行集成,以构建一个功能齐全且高效的 Web 开发环境。 2. **jQuery 版本选择**:为了兼容性和性能考虑...
cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip cdst ssh代码生成器,免费的,功能强悍的struts2+spring2.5+hibernate3.2的一套 代码生成系统。.zip cdst ssh代码...
Struts2.1.8.1、Spring3.0.2和Hibernate3.5.2是Java开发中经典的三大框架组合,它们各自扮演着不同的角色,共同构建了强大的企业级应用开发平台。在这个API集合中,包含了这三个框架的详细文档,帮助开发者深入理解...
标题 "compass-2.2.0+hibernate-3.2+struts-2.1.8.1+spring-framework-2.5.4" 指的是一个集成开发环境,它结合了四个关键的技术组件:Compass、Hibernate、Struts 2 和 Spring Framework,这些都是Java Web开发中的...
Struts2、Hibernate和Spring是Java Web开发中的三大框架,它们各自负责应用程序的不同层面:Struts2处理MVC(Model-View-Controller)架构中的控制层,Hibernate专注于对象关系映射(ORM),而Spring则提供了全面的...
在IT行业中,集成Struts2、iBatis2.x和Spring2.5是常见的企业级应用开发模式,这种架构被称为“SSH”(Struts、Spring、Hibernate)的变种,因为iBatis替代了传统的Hibernate作为持久层框架。Eclipse作为Java开发的...
4. **整合Struts2与Spring**:使用Spring插件Struts2-spring-plugin,让Struts2能够从Spring容器中获取Action实例,实现依赖注入。 5. **整合iBatis与Spring**:通过SqlSessionTemplate或SqlSessionDaoSupport,...
Struts2.3.16.1+Hibernate3.6.10+Spring3.2.8整合 能够运行,没有任何问题 另外附带applicationContext.xml、struts.xml、hibernate.cfg.xml
Struts2、Hibernate和Spring是Java开发中三大主流框架,它们各自在Web应用程序的不同层面上发挥作用,共同构建出高效、可维护的系统。本整合主要关注Spring 3.0.0版本,它作为轻量级的IoC(Inversion of Control,...
这个“struts2.1+spring2.5+hibernate3.2项目基本jar包”集合了这三个框架的核心库,为开发者提供了便捷的开发环境。 **Struts2** 是一个基于MVC设计模式的Web应用程序框架,主要用于控制应用程序的流程。Struts2.1...
Struts2.1、Hibernate3.2和Spring 2.5是Java开发中经典的MVC框架组合,它们各自承担着不同的职责。Struts2作为控制层,负责处理用户请求并调度业务逻辑;Hibernate则作为持久层框架,处理数据库操作;而Spring作为...