s2sh整合
此次整合的版本是:struts2.1.8+spring2.5.6+hibernate3.3.2
一.先整合hibernate和spring:
hibernate所需要jar包:antlr-2.7.6.jar、commons-collections-3.1.jar、dom4j-1.6.1.jar、hibernate3.jar、javassist-3.9.0.GA.jar、jta-1.1.jar、slf4j-api-1.5.8.jar、log4j.jar、slf4j-log4j12-1.5.8.jar(slf4j接口以log4j形式实现),因为采用了注解的方式所以还需(annotation3.4的包):ejb3-persistence.jar、hibernate-annotations.jar、hibernate-commons-annotations.jar、hibernate-entitymanager.jar,采用了c3p0连接池,还需要:c3p0-0.9.1.2.jar
spring所需要jar包:spring.jar、commons-logging.jar,spring注解需要:common-annotations.jar,aop需要:aspectjrt.jar、aspectjweaver.jar、cglib-nodep-2.1_3.jar
hibernate.cfg.xml配置:
Xml代码
1.<?xmlversion='1.0'encoding='UTF-8'?>
2.<!DOCTYPEhibernate-configurationPUBLIC
3."-//Hibernate/HibernateConfigurationDTD3.0//EN"
4."http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5.
6.<hibernate-configuration>
7.
8.<session-factory>
9.<propertyname="dialect">
10.org.hibernate.dialect.MySQLDialect
11.</property>
12.<propertyname="connection.url">
13.jdbc:mysql://localhost:3306/oa
14.</property>
15.<propertyname="connection.username">root</property>
16.<propertyname="connection.password">root</property>
17.<propertyname="connection.driver_class">
18.com.mysql.jdbc.Driver
19.</property>
20.<propertyname="myeclipse.connection.profile">mysql5</property>
21.<propertyname="show_sql">true</property>
22.<propertyname="current_session_context_class">thread</property>
23.<propertyname="hbm2ddl.auto">update</property>
24.
25.<!--首先说明我是使用c3p0连接池的方式-->
26.<propertyname="connection.provider_class">
27.org.hibernate.connection.C3P0ConnectionProvider
28.</property>
29.<!--最大连接数-->
30.<propertyname="hibernate.c3p0.max_size">20</property>
31.<!--最小连接数-->
32.<propertyname="hibernate.c3p0.min_size">2</property>
33.<!--获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒-->
34.<propertyname="hibernate.c3p0.timeout">5000</property>
35.<!--最大的PreparedStatement的数量-->
36.<propertyname="hibernate.c3p0.max_statements">100</property>
37.<!--每隔1000秒检查连接池里的空闲连接,单位是秒-->
38.<propertyname="hibernate.c3p0.idle_test_period">1000</property>
39.<!--当连接池里面的连接用完的时候,C3P0一下获取的新的连接数-->
40.<propertyname="hibernate.c3p0.acquire_increment">2</property>
41.<!--每次都验证连接是否可用-->
42.<propertyname="hibernate.c3p0.validate">true</property>
43.
44.<mappingclass="com.fsj.model.User"/>
45.
46.</session-factory>
47.
48.</hibernate-configuration>
<?xmlversion='1.0'encoding='UTF-8'?>
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<propertyname="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<propertyname="connection.url">
jdbc:mysql://localhost:3306/oa
</property>
<propertyname="connection.username">root</property>
<propertyname="connection.password">root</property>
<propertyname="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<propertyname="myeclipse.connection.profile">mysql5</property>
<propertyname="show_sql">true</property>
<propertyname="current_session_context_class">thread</property>
<propertyname="hbm2ddl.auto">update</property>
<!--首先说明我是使用c3p0连接池的方式-->
<propertyname="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<!--最大连接数-->
<propertyname="hibernate.c3p0.max_size">20</property>
<!--最小连接数-->
<propertyname="hibernate.c3p0.min_size">2</property>
<!--获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒-->
<propertyname="hibernate.c3p0.timeout">5000</property>
<!--最大的PreparedStatement的数量-->
<propertyname="hibernate.c3p0.max_statements">100</property>
<!--每隔1000秒检查连接池里的空闲连接,单位是秒-->
<propertyname="hibernate.c3p0.idle_test_period">1000</property>
<!--当连接池里面的连接用完的时候,C3P0一下获取的新的连接数-->
<propertyname="hibernate.c3p0.acquire_increment">2</property>
<!--每次都验证连接是否可用-->
<propertyname="hibernate.c3p0.validate">true</property>
<mappingclass="com.fsj.model.User"/>
</session-factory>
</hibernate-configuration>applicationContext.xml配置如下:
Xml代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<beansxmlns="http://www.springframework.org/schema/beans"
3.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
4.xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
5.xsi:schemaLocation="http://www.springframework.org/schema/beans
6.http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7.http://www.springframework.org/schema/context
8.http://www.springframework.org/schema/context/spring-context-2.5.xsd
9.http://www.springframework.org/schema/aop
10.http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
11.http://www.springframework.org/schema/tx
12.http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13.
14.<context:component-scanbase-package="com.fsj"/><!--启用自动扫描-->
15.<!--基于hibernate注解的sessionFactory-->
16.<beanid="sessionFactory"
17.class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
18.<propertyname="configLocation"value="classpath:hibernate.cfg.xml">
19.</property>
20.</bean>
21.<!--基于hibernate的事务管理器-->
22.<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
23.<propertyname="sessionFactory"ref="sessionFactory"/>
24.</bean>
25.<!--采用注解形式的声明式事务管理-->
26.<tx:annotation-driventransaction-manager="txManager"/>
27.
28.</beans>
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scanbase-package="com.fsj"/><!--启用自动扫描-->
<!--基于hibernate注解的sessionFactory-->
<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<propertyname="configLocation"value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!--基于hibernate的事务管理器-->
<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
<!--采用注解形式的声明式事务管理-->
<tx:annotation-driventransaction-manager="txManager"/>
</beans>
User类采用hibernate的注解形式:
Java代码
1.packagecom.fsj.model;
2.
3.importjavax.persistence.Column;
4.importjavax.persistence.Entity;
5.importjavax.persistence.GeneratedValue;
6.importstaticjavax.persistence.GenerationType.IDENTITY;
7.importjavax.persistence.Id;
8.importjavax.persistence.Table;
9.
10.@Entity
11.@Table(name="_user")
12.publicclassUserimplementsjava.io.Serializable{
13.
14.privatestaticfinallongserialVersionUID=-3564872478826196506L;
15.privateIntegeruid;
16.privateStringuname;
17.privateStringupwd;
18.
19.@Id
20.@GeneratedValue(strategy=IDENTITY)
21.@Column(name="u_id",unique=true,nullable=false)
22.publicIntegergetUid(){
23.returnthis.uid;
24.}
25.
26.publicvoidsetUid(IntegerUId){
27.this.uid=UId;
28.}
29.
30.@Column(name="u_name",length=20)
31.publicStringgetUname(){
32.returnthis.uname;
33.}
34.
35.publicvoidsetUname(StringUName){
36.this.uname=UName;
37.}
38.
39.@Column(name="u_pwd",length=20)
40.publicStringgetUpwd(){
41.returnthis.upwd;
42.}
43.
44.publicvoidsetUpwd(StringUPwd){
45.this.upwd=UPwd;
46.}
47.
48.}
packagecom.fsj.model;
importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importstaticjavax.persistence.GenerationType.IDENTITY;
importjavax.persistence.Id;
importjavax.persistence.Table;
@Entity
@Table(name="_user")
publicclassUserimplementsjava.io.Serializable{
privatestaticfinallongserialVersionUID=-3564872478826196506L;
privateIntegeruid;
privateStringuname;
privateStringupwd;
@Id
@GeneratedValue(strategy=IDENTITY)
@Column(name="u_id",unique=true,nullable=false)
publicIntegergetUid(){
returnthis.uid;
}
publicvoidsetUid(IntegerUId){
this.uid=UId;
}
@Column(name="u_name",length=20)
publicStringgetUname(){
returnthis.uname;
}
publicvoidsetUname(StringUName){
this.uname=UName;
}
@Column(name="u_pwd",length=20)
publicStringgetUpwd(){
returnthis.upwd;
}
publicvoidsetUpwd(StringUPwd){
this.upwd=UPwd;
}
}DAO层的实现类为:
Java代码
1.packagecom.fsj.dao.impl;
2.
3.importjava.util.List;
4.
5.importjavax.annotation.Resource;
6.
7.importorg.hibernate.SessionFactory;
8.importorg.springframework.orm.hibernate3.HibernateTemplate;
9.importorg.springframework.stereotype.Repository;
10.
11.importcom.fsj.dao.UserDao;
12.importcom.fsj.model.User;
13.
14.@Repository("userDao")
15.publicclassUserDaoImplimplementsUserDao{
16.
17.privateHibernateTemplatehibernateTemplate;
18.
19.@Resource
20.publicvoidsetSessionFactory(SessionFactorysessionFactory)
21.{
22.this.hibernateTemplate=newHibernateTemplate(sessionFactory);
23.}
24.@SuppressWarnings("unchecked")
25.publicUserlogin(Stringname,Stringpwd){
26.List<User>users=hibernateTemplate.find("fromUserwhereuname=?andupwd=?",newObject[]{name,pwd});
27.System.out.println(users.size()+"-------------");
28.return(users==null||users.size()==0)?null:(User)users.get(0);
29.}
30.
31.}
packagecom.fsj.dao.impl;
importjava.util.List;
importjavax.annotation.Resource;
importorg.hibernate.SessionFactory;
importorg.springframework.orm.hibernate3.HibernateTemplate;
importorg.springframework.stereotype.Repository;
importcom.fsj.dao.UserDao;
importcom.fsj.model.User;
@Repository("userDao")
publicclassUserDaoImplimplementsUserDao{
privateHibernateTemplatehibernateTemplate;
@Resource
publicvoidsetSessionFactory(SessionFactorysessionFactory)
{
this.hibernateTemplate=newHibernateTemplate(sessionFactory);
}
@SuppressWarnings("unchecked")
publicUserlogin(Stringname,Stringpwd){
List<User>users=hibernateTemplate.find("fromUserwhereuname=?andupwd=?",newObject[]{name,pwd});
System.out.println(users.size()+"-------------");
return(users==null||users.size()==0)?null:(User)users.get(0);
}
}
service层的实现类采用事务管理:
Java代码
1.packagecom.fsj.sevice.impl;
2.
3.importjava.util.List;
4.
5.importjavax.annotation.Resource;
6.
7.importorg.springframework.stereotype.Service;
8.importorg.springframework.transaction.annotation.Propagation;
9.importorg.springframework.transaction.annotation.Transactional;
10.
11.importcom.fsj.dao.UserDao;
12.importcom.fsj.model.User;
13.importcom.fsj.sevice.UserService;
14.
15.@Service("userService")
16.@Transactional(rollbackFor=Exception.class)
17.publicclassUserServiceImplimplementsUserService{
18.
19.@Resource
20.privateUserDaouserDao;
21.@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
22.publicUserlogin(Stringname,Stringpwd){
23.returnuserDao.login(name,pwd);
24.}
25.
26.}
packagecom.fsj.sevice.impl;
importjava.util.List;
importjavax.annotation.Resource;
importorg.springframework.stereotype.Service;
importorg.springframework.transaction.annotation.Propagation;
importorg.springframework.transaction.annotation.Transactional;
importcom.fsj.dao.UserDao;
importcom.fsj.model.User;
importcom.fsj.sevice.UserService;
@Service("userService")
@Transactional(rollbackFor=Exception.class)
publicclassUserServiceImplimplementsUserService{
@Resource
privateUserDaouserDao;
@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
publicUserlogin(Stringname,Stringpwd){
returnuserDao.login(name,pwd);
}
}
junit测试成功后,再加入struts2:
加入包:xwork-core-2.1.6.jar、struts2-core-2.1.8.jar、ognl-2.7.3.jar、freemarker-2.3.15.jar、commons-io-1.3.2.jar、commons-fileupload-1.2.1.jar、struts2-spring-plugin-2.1.8.jar
web.xml如下设置:
Xml代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<web-appversion="2.5"
3.xmlns="http://java.sun.com/xml/ns/javaee"
4.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5.xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6.http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
7.
8.<context-param>
9.<param-name>contextConfigLocation</param-name>
10.<param-value>classpath:applicationContext.xml</param-value>
11.</context-param>
12.
13.<listener>
14.<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15.</listener>
16.
17.
18.<filter>
19.<filter-name>OpenSessionInViewFilter</filter-name>
20.<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
21.</filter>
22.<filter-mapping>
23.<filter-name>OpenSessionInViewFilter</filter-name>
24.<url-pattern>/*</url-pattern>
25.</filter-mapping>
26.
27.<filter>
28.<filter-name>struts2</filter-name>
29.<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
30.</filter>
31.<filter-mapping>
32.<filter-name>struts2</filter-name>
33.<url-pattern>/*</url-pattern>
34.</filter-mapping>
35.
36.<filter>
37.<filter-name>encoding</filter-name>
38.<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
39.<init-param>
40.<param-name>encoding</param-name>
41.<param-value>UTF-8</param-value>
42.</init-param>
43.</filter>
44.<filter-mapping>
45.<filter-name>encoding</filter-name>
46.<url-pattern>/*</url-pattern>
47.</filter-mapping>
48.
49.</web-app>
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml如下:
Xml代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<!DOCTYPEstrutsPUBLIC
3."-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
4."http://struts.apache.org/dtds/struts-2.0.dtd">
5.
6.<struts>
7.
8.<!--指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法-->
9.<constantname="struts.i18n.encoding"value="UTF-8"/>
10.<!--设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭-->
11.<constantname="struts.serve.static.browserCache"value="false"/>
12.<!--当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
13.<constantname="struts.configuration.xml.reload"value="true"/>
14.<!--开发模式下使用,这样可以打印出更详细的错误信息-->
15.<constantname="struts.devMode"value="true"/>
16.<!--默认的视图主题-->
17.<constantname="struts.ui.theme"value="simple"/>
18.<!--把action对象交给spring创建-->
19.<constantname="struts.objectFactory"value="spring"/>
20.
21.<packagename="myDefault"extends="struts-default">
22.<default-action-refname="indexPage"/>
23.<global-results>
24.<resultname="exceptionPage">/WEB-INF/exceptionPage.jsp
25.</result>
26.</global-results>
27.<global-exception-mappings>
28.<exception-mappingresult="exceptionPage"exception="java.lang.Exception"/>
29.</global-exception-mappings>
30.<actionname="indexPage">
31.<result>/login.jsp</result>
32.</action>
33.</package>
34.
35.<packagename="user"namespace="/user"extends="myDefault">
36.<!--这里面的class不是指完整类路径,而是指在spring中定义的bean的名称-->
37.<actionname="*UserAction"class="userAction"method="{1}">
38.<resultname="success">/WEB-INF/user/loginSuccess.jsp</result>
39.<resultname="input">/login.jsp</result>
40.</action>
41.</package>
42.
43.</struts>
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法-->
<constantname="struts.i18n.encoding"value="UTF-8"/>
<!--设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭-->
<constantname="struts.serve.static.browserCache"value="false"/>
<!--当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
<constantname="struts.configuration.xml.reload"value="true"/>
<!--开发模式下使用,这样可以打印出更详细的错误信息-->
<constantname="struts.devMode"value="true"/>
<!--默认的视图主题-->
<constantname="struts.ui.theme"value="simple"/>
<!--把action对象交给spring创建-->
<constantname="struts.objectFactory"value="spring"/>
<packagename="myDefault"extends="struts-default">
<default-action-refname="indexPage"/>
<global-results>
<resultname="exceptionPage">/WEB-INF/exceptionPage.jsp
</result>
</global-results>
<global-exception-mappings>
<exception-mappingresult="exceptionPage"exception="java.lang.Exception"/>
</global-exception-mappings>
<actionname="indexPage">
<result>/login.jsp</result>
</action>
</package>
<packagename="user"namespace="/user"extends="myDefault">
<!--这里面的class不是指完整类路径,而是指在spring中定义的bean的名称-->
<actionname="*UserAction"class="userAction"method="{1}">
<resultname="success">/WEB-INF/user/loginSuccess.jsp</result>
<resultname="input">/login.jsp</result>
</action>
</package>
</struts>
UserAction类:
Java代码
1.packagecom.fsj.web;
2.
3.importjava.util.Map;
4.
5.importjavax.annotation.Resource;
6.
7.importorg.apache.struts2.interceptor.SessionAware;
8.importorg.springframework.context.annotation.Scope;
9.importorg.springframework.stereotype.Controller;
10.
11.importcom.fsj.model.User;
12.importcom.fsj.sevice.UserService;
13.importcom.opensymphony.xwork2.ActionSupport;
14.@Controller("userAction")@Scope("prototype")
15.publicclassUserActionextendsActionSupportimplementsSessionAware{
16.
17.privateMap<String,Object>session;
18.privateUseruser;
19.@Resource
20.privateUserServiceuserService;
21.
22.publicStringgetResult(){
23.returnresult;
24.}
25.
26.publicStringlogin()
27.{
28.UserlogindUser=userService.login(user.getUname(),user.getUpwd());
29.if(logindUser==null)
30.{
31.addActionError("用户名或密码错误,请重新输入。");
32.returnINPUT;
33.}
34.session.put("user",logindUser);
35.returnSUCCESS;
36.}
37.
38.publicUsergetUser(){
39.returnuser;
40.}
41.
42.publicvoidsetUser(Useruser){
43.this.user=user;
44.}
45.
46.publicvoidsetSession(Map<String,Object>session){
47.this.session=session;
48.}
49.}
packagecom.fsj.web;
importjava.util.Map;
importjavax.annotation.Resource;
importorg.apache.struts2.interceptor.SessionAware;
importorg.springframework.context.annotation.Scope;
importorg.springframework.stereotype.Controller;
importcom.fsj.model.User;
importcom.fsj.sevice.UserService;
importcom.opensymphony.xwork2.ActionSupport;
@Controller("userAction")@Scope("prototype")
publicclassUserActionextendsActionSupportimplementsSessionAware{
privateMap<String,Object>session;
privateUseruser;
@Resource
privateUserServiceuserService;
publicStringgetResult(){
returnresult;
}
publicStringlogin()
{
UserlogindUser=userService.login(user.getUname(),user.getUpwd());
if(logindUser==null)
{
addActionError("用户名或密码错误,请重新输入。");
returnINPUT;
}
session.put("user",logindUser);
returnSUCCESS;
}
publicUsergetUser(){
returnuser;
}
publicvoidsetUser(Useruser){
this.user=user;
}
publicvoidsetSession(Map<String,Object>session){
this.session=session;
}
}
与UserAction同一包中建立验证文件:UserAction-validation.xml
Xml代码
1.<?xmlversion="1.0"encoding="UTF-8"?>
2.<!DOCTYPEvalidatorsPUBLIC
3."-//OpenSymphonyGroup//XWorkValidator1.0.2//EN"
4."http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
5.
6.<validators>
7.<fieldname="user.uname">
8.<field-validatortype="requiredstring">
9.<message>登录的用户名不能为空</message>
10.</field-validator>
11.<field-validatortype="regex">
12.<paramname="expression">^[a-zA-Z][a-zA-Z0-9_]{3,14}$</param>
13.<message>登录的用户名必须以字母开始,后面跟字母、数字或_,长度为4-15位</message>
14.</field-validator>
15.</field>
16.
17.<fieldname="user.upwd">
18.<field-validatortype="requiredstring">
19.<message>登录的密码不能为空</message>
20.</field-validator>
21.<field-validatortype="regex">
22.<paramname="expression">^[a-zA-Z0-9!@#]{4,15}$</param>
23.<message>登录的密码可以由字母、数字和!@#组成,长度为4-15位</message>
24.</field-validator>
25.</field>
26.</validators>
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEvalidatorsPUBLIC
"-//OpenSymphonyGroup//XWorkValidator1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<fieldname="user.uname">
<field-validatortype="requiredstring">
<message>登录的用户名不能为空</message>
</field-validator>
<field-validatortype="regex">
<paramname="expression">^[a-zA-Z][a-zA-Z0-9_]{3,14}$</param>
<message>登录的用户名必须以字母开始,后面跟字母、数字或_,长度为4-15位</message>
</field-validator>
</field>
<fieldname="user.upwd">
<field-validatortype="requiredstring">
<message>登录的密码不能为空</message>
</field-validator>
<field-validatortype="regex">
<paramname="expression">^[a-zA-Z0-9!@#]{4,15}$</param>
<message>登录的密码可以由字母、数字和!@#组成,长度为4-15位</message>
</field-validator>
</field>
</validators>
login.jsp页面:
Jsp代码
1.<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
2.<%@taglibprefix="s"uri="/struts-tags"%>
3.<%
4.Stringpath=request.getContextPath();
5.StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
6.%>
7.
8.<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
9.<html>
10.<head>
11.<basehref="<%=basePath%>">
12.<title>登陆</title>
13.</head>
14.
15.<body>
16.<s:head/>
17.<s:actionerror/>
18.<s:fielderror/>
19.<s:formaction="loginUserAction"namespace="/user"method="post">
20.<s:textfieldlabel="用户名"name="user.uname"/>
21.<s:passwordlabel="密码"name="user.upwd"/>
22.<s:submitvalue="登陆"/>
23.</s:form>
24.</body>
25.</html>