1 Why you need dispatchaction.
If each request corresponds to an action, it causes too much action and the program looks bloated, so the program needs to lose weight (you can write a type of request to handle in an action), so the program is relatively concise and facilitates maintenance and expansion. 2 dispatchaction Development
create loginandlogoutaction inherit from Dispatchaction
Add Parameter Property in ①struts-config.xml
<!--parameter is used to differentiate users from different requests--
<action
attribute= "UserForm"
input= "/web-inf/ok.jsp"
name = "UserForm"
parameter= "flag"
path= "/loginandlogout"
scope= "request"
type= " Com.test.struts.action.LoginAndLogoutAction "
validate=" false "
cancellable=" true ">
<!-- Partial jump--
<forward name= "Gologin" path= "/web-inf/login.jsp"/> <forward
name= "Loginok" path= "/ web-inf/ok.jsp "/>
</action>
② corresponding loginandlogoutaction file
public class Loginandlogoutaction extends Dispatchaction {//Use assign action, often need to rename function yourself//This function is used in response to login request public A Ctionforward Userlogin (actionmapping mapping, actionform form, httpservletrequest request, HttpServletResponse Response) {UserForm UserForm = (UserForm) form;//TODO auto-generated method stub if ("123". Equals (Userf
Orm.getpassword ())) {return Mapping.findforward ("Loginok");
} else {return Mapping.findforward ("Gologin"); }}//This function is used to respond to logoff requests public Actionforward userlogout (actionmapping mapping, actionform form, Httpser Vletrequest request, HttpServletResponse response) {UserForm UserForm = (UserForm) form;//TODO auto-generated Me
Thod stub//emptying Session request.getsession (). invalidate (); Return Mapping.findforward ("Goerr");
Global Jump}//This function is used to respond to logoff requests public Actionforward USERLOGOUT2 (actionmapping mapping, Actionform form, HTtpservletrequest request, HttpServletResponse response) {UserForm UserForm = (UserForm) form;//TODO Auto-genera
Ted method Stub//emptying Session System.out.println ("Logout2");
Request.getsession (). invalidate ();
Return Mapping.findforward ("Gologin"); }
}
③ URL format for jumps
/dispatchtest/loginandlogout.do? flag=Userlogin
<form action= "/dispatchtest/loginandlogout.do?flag=userlogin" method= "POST" >
u:<input type= "text" Name= "Name" ><br>
p:<input type= "password" name= "password" ><br>
<input type= " Submit "value=" Login >
</form>
3 global jumps and local jumps
Global jump: Refers to all the action can jump to the page;
<global-forwards >
<forward name= "Goerr" path= "/web-inf/err.jsp"/>
</global-forwards>
Local jump: means that only this action can jump to the page.
<action attribute= "UserForm" input= "/web-inf/ok.jsp" name= "UserForm" parameter= "flag" Path= "/loginandlogout" scope= "Request" type= "Com.test.struts.action.LoginAndLogoutAction" validate= "false" Cance Llable= "true" > <!--partial jump--<forward name= "Gologin" path= "/web-inf/login.jsp"/> <forward name= "l Oginok "path="/web-inf/ok.jsp "/> </action>