Struts 2在JSP中获取结果字符串

时间:2013-12-14 03:07:28

标签: java jsp tomcat struts2 ognl

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

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

只有静态访问可用

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

要使此代码能够工作,您需要一个配置

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