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>   

  • 相关阅读:
    语言模型的压缩方法
    推荐算法之 Slope One 算法
    基于内容的推荐(Contentbased Recommendations)
    Txt文件转换为Excel文件
    WebResource 内嵌资源
    多层模态窗口showModalDialog页面提交及刷新
    屏蔽/自定义JavaScript脚本错误
    .net动态显示当前时间
    客户端自动累加
    中国IT管理之窥豹一斑
  • 原文地址:https://www.cnblogs.com/moonfans/p/3021415.html
Copyright © 2011-2022 走看看