【问题标题】:Struts 2 get result string in JSPStruts 2 在 JSP 中获取结果字符串
【发布时间】:2013-12-14 03:07:27
【问题描述】:

JSP中如何获取动作结果字符串? 我想对所有结果使用相同的 JSP。

public String doSth()
{
    return a ? "ok" : "failed";
}

【问题讨论】:

    标签: java jsp tomcat struts2 ognl


    【解决方案1】:

    same.jsp:

        <%-- 
            Author     : Prabhakar
        --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
    
    <html>
        <body>    
              The Result is : <s:property value="a" />
        </body>
    </html>
    

    ActionClass.java:

    package com.prabhakar;
    import com.opensymphony.xwork2.ActionSupport;
    import java.util.Map;
    import org.apache.struts2.interceptor.SessionAware;
    
    /**
     *
     * @author Prabhakar
     */
    public class ActionClass extends ActionSupport {
    
        private String a;
    
        public String getA() {
            return this.a;
        }
    
        public void setA(String a) {
            this.a = a;
        }
    
        public String doSth() {
            if (some condition) {
                a = "success";
            } else {
                a = "failed";
            }
            return a;
        }
    }
    

    struts.xml

    <struts>
      <package name="somename" extends="struts-default">
      <action name="resultName" method="doSth" class="com.prabhakar.ActionClass">
       <result name="success">same.jsp</result>
        <result name="failed">same.jsp</result>
     </action>
    </package>
    </struts>
    

    【讨论】:

      【解决方案2】:

      只有静态访问可用

      <s:property value="@com.opensymphony.xwork2.ActionContext@getContext().getActionInvocation().getResultCode()"/>
      

      为了让这段代码能够工作,你需要一个配置

      <constant name="struts.ognl.allowStaticMethodAccess" value="true" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-15
        • 1970-01-01
        • 2013-05-15
        • 2018-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多