Struts (7) Struts Dispatchaction (dispatch action) and global jump

Source: Internet
Author: User
Tags stub
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> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.