我采用的技术是:struts2.1 + hibernate 3.3 +spring 3.0.
我有一个用户登录界面需要验证用户名是否为空。假如我没有输入用户名, 点击登录, 则进入错误提示界面: 用户名不能为空。
然后我返回登录界面, 仍然不输入用户名, 点击登录, 则进入错误提示界面: 显示2行 用户名不能为空。
我重复N次, 则显示N行: 用户名不能为空。error.jsp内容:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%><s:fielderror/>
has error验证文件UsersAction-login-validation.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
<validators>
<field name="uname">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>user name cannot be null</message>
</field-validator>
</field>
</validators>web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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>
<listener>  
<listener-class>  
org.springframework.web.context.ContextLoaderListener   
</listener-class>  
</listener>  
  </web-app>structs.xml内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <constant name="struts.objectFactory" value="spring"></constant>
 <package name="com.login" extends="struts-default">
 <action name="login" class="UsersAction" method="login">
 <result name="success">/index.jsp</result>
 <result name="input">/error.jsp</result>
 <result name="error">/error.jsp</result>
 </action>
 </package>
</struts>    
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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url"
value="jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=testdb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/login/Groups.hbm.xml</value>
<value>com/login/Users.hbm.xml</value></list>
</property></bean>
<bean id="transactionManager"    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <property name="sessionFactory">
 <ref bean="sessionFactory" />
 </property>
</bean>
<bean id="GroupsDAO" class="com.login.GroupsDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="UsersDAO" class="com.login.UsersDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="UsersService" class="com.login.UsersService">
 <property name="usersDAO" ref="UsersDAO" />
 <property name="groupsDAO" ref="GroupsDAO" />
</bean>
<bean id="UsersAction" class="com.login.UsersAction">
 <property name="usersService" ref="UsersService" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
 <tx:attributes>
 <tx:method name="save*" propagation="REQUIRED" />
 <tx:method name="update*" propagation="REQUIRED" />
 <tx:method name="delete*" propagation="REQUIRED" />
 <tx:method name="add*" propagation="REQUIRED" />
 <tx:method name="del*" propagation="REQUIRED" />
 <tx:method name="*" read-only="true" />
 </tx:attributes>
</tx:advice>
<aop:config>
 <aop:pointcut id="allServiceMethod" expression="execution(* app.service.*.*(..))" />
 <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
</aop:config>
</beans>我觉得可能是UsersAction-login-validation.xml中出了问题, 是不是那个TAG的某个特性我没有设有设?
现在的问题是在哪里加上clearFieldErrors();, 我在UsersAction的构造函数中加上,不行。
或者在UsersAction-login-validation.xml可以配置一下?