Struts2一直回来input页面

Struts2一直返回input页面

昨天弄了个简单的注册,出现一个小问题,今天记录下来,下次再出现也好查找。

这个工程基于Struts2+Spring+Hibernate,问题如下:

 

1. 注册页面直接访问/user/regist.jsp,出现异常org.apache.jasper.JasperException:

 

org.apache.jasper.JasperException: The Struts dispatcher cannot be found.  
	This is usually caused by using Struts tags without the associated filter
	Struts tags are only usable when the request has passed through its servlet filter, 
	which initializes the Struts dispatcher needed for this tag.

提示很清楚,使用struts2标签没有使用相关的过滤器(^_^就是struts2的过滤器了)。web.xml配置Struts2的处理器可以处理.jsp的请求。配置方法就不多说了。

 

2.再次访问jsp,后台有struts日志打印出的警告信息No configuration found for the specified action:

No configuration found for the specified action: 'regist.do' in namespace: '/user'. Form action defaulting to 'action' attribute's literal value.

在命名空间/user下找不到regist.do。先贴出这段配置:

regist.jsp中form配置

 

<s:form action="regist.do" theme="simple" onsubmit="return checkRegist()">
		...
</s:form>

 

 struts2中action配置

 

<package name="user" namespace="/user" extends="struts-default">
	<action name="regist" method="regist" class="userAction">
		<result name="input">/user/regist.jsp</result>
		<result name="success">/result/message.jsp</result>
	</action>
</package>

 

 实际上是有配置regist这个action的,为什么找不到呢??

注意,是提示regist.do找不到,因为.do这个action确实是没有。struts标签会自动帮我们补全action的路径,并且加上struts2处理的结尾字符串(如".action"或".do"),所以去掉action中的结尾".do"。

再次请求jsp,OK!

 

3. 蛋疼的问题:一次验证失败后一直返回input配置的页面

action的regist方法

 

public String regist(){
		if(!userDao.isUserExist(user)){
			System.out.println("...注册用户:"+user);
			userDao.save(user);
			return SUCCESS;
		}else{
			System.out.println("用户已经存在:"+user);
			addFieldError("user.userName", getText("form.userNameExist"));
			//用户名已存在
			return INPUT;
		}
	}

 

 当注册同一个用户失败后,再次提交,发现总是返回配置的input页面,而且后台根本就没打印2种情况下应该打印的字符串,这是神马情况??

今天回来或立马开始查找问题原因:

首先,没有使用验证框架或者action中重写验证方法,那就不会是验证失败返回了!

偶然在网上看到一篇,情况一样,也有使用addFieldError()类似的方法,有网友回帖说要清了errors,为什么呢?虽然没有更多的说明,不过也提示了我。

因为spring给你的bean默认是singleton的,第一次出错了,再返回来的时候,发现仍然有errors,struts2于是就不假思索的直接返回给你input配置的页面了,然后呢就是不管你怎么弄都是返回input的页面。

配置action的scope为prototype。第一次注册重复的,再多次注册不同的全部OK!解决问题。