The common technique of struts of SSH framework--data echo, prevent form repeating commit

Source: Internet
Author: User
Tags commit constant ssh

The three major techniques used in STRUTS2:
1. Data echo
2. Model-driven
3. Prevent duplicate submission of forms

First, data echo:
1, data echo, you must use the struts tag.

2. Code Explanation:
1) Action:

Go to modify page public
    String viewupdate () {
        ///Impersonate an object (get an ID first, and then call the service query based on the ID to save the found result to the domain)
        User userInfo = new User ();
        Userinfo.setusername ("Endeavor");
        Userinfo.setemail ("Endeavor@163.com");

        Actioncontext AC = Actioncontext.getcontext ();      map<string,object> request = (map<string, object>) ac.get ("request");      request.put ("UserInfo", userInfo);

        /************* data echo ***************/
        //Get value stack
        valuestack vs = Ac.getvaluestack ();
        Vs.pop ();//Remove stack top element
        Vs.push (userInfo);  into


        the stack//Enter
        the change page return "Viewupdate";
    }

2) JSP page:

<body>
    <% @taglib uri= "/struts-tags" prefix= "s"%>


    <br/>
    <!--in the page text box, display the data to modify the record-- >

    <!--manually set the value displayed by value 
    <s:form action= "#" >

        user name: <s:textfield name= "User.username" value= "%{#request. Userinfo.username}" ></s:textfield>   <br/>

        Email: <s:textfield name= "User.email "Value="%{#request. Userinfo.email} "></s:textfield>     <br/>
    </s:form>
    -

    <!--data echo technology: S:textfield automatically finds root element data (OGNL expression language value)--
    <s:form action= "#" >

        Username: < S:textfield name= "UserName" ></s:textfield>   <br/>

        Email: <s:textfield name= "Email" > </s:textfield>     <br/>
    </s:form>

    <s:debug></s:debug>
  </body >

Ii. prevent duplicate submissions of forms
Core: Struts provides protection against form repeat-commit interceptors:

<interceptor name= "token" 
class= "Org.apache.struts2.interceptor.TokenInterceptor"/>

Explain it through the case:
1, update.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% @taglib uri= "/struts-tags" prefix= "s" %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

2, list.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% @taglib uri= "/struts-tags" prefix= "s" %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

3, add.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% @taglib uri= "/struts-tags" prefix= "s" %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

4, Employeeaction.java

public class Employeeaction extends Actionsupport implements modeldriven<employee>{/**** package data ****/Private
    Employee Employee = new Employee ();
    Public employee GetEmployee () {return employee;
    } public void Setemployee (employee employee) {This.employee = employee;
    }//Overriding the model-driven method @Override public employee Getmodel () {return employee;

    }/**** called service****/private iemployeeservice EmployeeService = new EmployeeService (); /** * 1. Add Employee */public String Save () {try {//Call service Save Employeeservice.save (employee
            );
            Add the success, go to the list page return lists ();
        return "Addsuccess";
            } catch (Exception e) {e.printstacktrace ();
        return ERROR; }}/** * 2. List Display */public String list () {try {//query all list<employee> listemp = Emplo Yeeservice.getall ();
            Save to request Domain Actioncontext.getcontext (). Getcontextmap (). Put ("Listemp", listemp);
        return "List";
            } catch (Exception e) {e.printstacktrace ();
        return ERROR; }}/** * 3. Go to modify page */public String viewupdate () {try {//3.1 Gets the primary key value of the currently modified record int id = employ
            Ee.getid ();
            3.2 Service Query Employee EMP = Employeeservice.findbyid (ID);
            3.3 Data echo//A. First get the value stack valuestack vs = Actioncontext.getcontext (). Getvaluestack ();   Vs.pop ();   Remove stack top element Vs.push (EMP);
        The EMP object is placed on top of the stack return "update";
            } catch (Exception e) {e.printstacktrace ();
        return ERROR; }}/** * 4. Modify Employee */Public String update () {try {//Invoke service modification Employeeservice.update (Empl
            Oyee);
        return list ();} catch (Exception e) {e.printstacktrace ();
        return ERROR; }
    }
}

5, Struts.xml

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/ Dtds/struts-2.3.dtd > <struts> <!--change Theme--<constant name= "Struts.ui.theme" value= "simple" ;</constant> <package name= "emp" extends= "Struts-default" > <!--Global View--<globa

        l-results> <result name= "error" >/error/error.jsp</result> </global-results> <action name= "emp_*" class= "cn.itcast.action.EmployeeAction" method= "{1}" > <!--Prevent forms from repeating commits, step two: Configure "Prevent forms from repeating the interceptor"--<interceptor-ref name= "Defaultstack" ></interceptor-ref> <inte Rceptor-ref name= "token" > <!--Specifies which methods need to be blocked from repeating the form (save)--<param name= "inclu Demethods ">save</param> </interceptor-ref> <!--prevent formsRepeat commit, step three: If the user repeatedly commits a jump to the specified error page--<result name= "Invalid.token" type= "Redirectaction" >emp_list</re

            Sult> <!--home Display-<result name= "list" >/WEB-INF/list.jsp</result> 
            <!--go to the edit page-<result name= "Update" >/WEB-INF/update.jsp</result> <!-- <result name= "addsuccess" type= "redirectaction" >emp_list</result>-&lt ;/action> </package> </struts>

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.