解决struts1中请求跳转到Action而非execute的问题
代码:
<html:form action="/loginaction.do" >
<input type="hidden" name="method" value="login" />
<html:hidden property="id" />
用户名:<html:text property="uname" /> <br>
密 码:<html:password property="upass" /><br>
<html:submit/>
</html:form>
struts-config.xml 代码:
<struts-config>
<form-beans>
<form-bean name="userinfo" type="userinfo" />
</form-beans>
<action-mappings>
<action path="/loginaction" type="loginaction" name="userinfo" scope="request" parameter="method">
<forward name="success" path="/success.jsp" />
<forward name="error" path="/error.jsp" />
</action>
</action-mappings>
</struts-config>
java 代码:
public class loginaction extends dispatchaction
{
public actionforward login(actionmapping mapping, actionform form,
httpservletrequest request, httpservletresponse response)
throws exception
{
userinfo dform = (userinfo) form;
string uname = dform.getuname();
string upass = dform.getupass();
system.out.println(uname + " login " + upass);
return mapping.findforward("success");
}
@override
public actionforward execute(actionmapping mapping, actionform form,
httpservletrequest request, httpservletresponse response)
throws exception
{
userinfo dform = (userinfo) form;
string uname = dform.getuname();
string upass = dform.getupass();
system.out.println(uname + " execute " + upass);
return mapping.findforward("success");
}
}
在网上查了一些相关的资料,得知是在struts-config.xml中添加parameter=”method”。
在页面添加<input type=”hidden” name=”method” value=”login” /> 就可以了!
上一篇: 非服务器控件绑定数据
下一篇: 用PS去除图片中的水印或者文字技巧介绍