zoukankan      html  css  js  c++  java
  • struts2中多个逻辑action(方法)的动态调用

    Struts2可以实现在同一个action中通过多个方法实现在配置文件中配置出多个逻辑的action,通过在js中给各个button添加onclick方法调用实现提交到不同的action。可以实现多个按钮的动态提交。并且可以通过在action中设置参数属性在struts.xml文件中的result中配置表达式来动态跳转到不同的逻辑视图

    package com.inspur.action;

    import java.util.Map;

    import com.opensymphony.xwork2.ActionContext;

    import com.opensymphony.xwork2.ActionSupport;

    public class MyAction extends ActionSupport {

             private String username;

             private String password;

            

             public String getUsername() {

                       return username;

             }

             public void setUsername(String username) {

                       this.username = username;

             }

             public String getPassword() {

                       return password;

             }

             public void setPassword(String password) {

                       this.password = password;

             }

             private String target;

             private String tip;

             public String getTarget() {

                       return target;

             }

             public void setTarget(String target) {

                       this.target = target;

             }

             public String getTip() {

                       return tip;

             }

             public void setTip(String tip) {

                       this.tip = tip;

             }

             public String login(){

                       if(this.getUsername().equals("lzhq")&&this.getPassword().equals("123")){

                                ActionContext actionContext=ActionContext.getContext();

                                Map session=actionContext.getSession();

                                session.put("user", this.getUsername());

                                return SUCCESS;

                       }else

                      

                       return ERROR;

             }

             public String direct(){

                       if(this.getTarget().equals("welcome"))

                                return SUCCESS;

                       else

                       return ERROR;

             }

    }

    在struts.xml文件中通过属性 值在result中配置表达式实现动态跳转

    <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

    <struts>

        <package name="demo2" extends="struts-default" namespace="/">

           <action name="login" class="com.inspur.action.MyAction" method="login">

               <result name="success">/success.jsp</result>

               <result name="error">/loginFail.jsp</result>

           </action>

           <action name="direct" class="com.inspur.action.MyAction" method="direct">

               <result name="success">/${target}.jsp</result>

               <result name="error">/directFault.jsp</result>

           </action>

        </package>

     

    </struts>   

  • 相关阅读:
    HDU3145 Max Sum of Max-K-sub-sequence (单调队列模板)
    AcWing1088 旅行问题(单调队列)
    POJ1821 Fence(单调队列)
    POJ1742 Coins(多重背包+二进制优化)
    AcWing217 绿豆蛙的归宿(期望)
    BZOJ.2134.[国家集训队]单选错位(概率 递推)
    洛谷.3805.[模板]manacher算法
    Codeforces.280C.Game on Tree(期望)
    BZOJ.2521.[SHOI2010]最小生成树(最小割ISAP/Dinic)
    洛谷.4172.[WC2006]水管局长(LCT Kruskal)
  • 原文地址:https://www.cnblogs.com/moonfans/p/3021415.html
Copyright © 2011-2022 走看看