欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决struts1中请求跳转到Action而非execute的问题

程序员文章站 2022-04-15 13:28:48
struts1中怎么让请求跳转到指定的action而非execute呢? 代码如下: 代码:
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” /> 就可以了!