1

我尝试将诸如( Save 或 Edit )之类的动作信息从我的 struts2 动作类( CommonAction.java )传递到 jsp 文件( test.jsp )。

在动作类中,我使用下面的代码来设置动作值。

request.setAttribute("action","Save");

当我尝试在 jsp 文件中获取 action 属性时。

String actionValue = (String)request.getAttribute("action");

我在我的 jsp 文件中获取操作类而不是操作值。

下面是异常跟踪。

java.lang.ClassCastException: com.actions.CommonAction cannot be cast to java.lang.String
at org.apache.jsp.jsp.common.test_jsp._jspService(test_jsp.java:119)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:164)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

有什么方法可以在不更改属性名称的情况下获取操作值?

4

2 回答 2

3

此功能由StrutsRequestWrapper. 它覆盖该getAttribute()方法以支持在 OGNL 值堆栈中搜索属性。您可以通过在 struts.xml 中设置以下内容来禁用该功能:

<constant name="struts.disableRequestAttributeValueStackLookup" value="true"/>
于 2013-07-16T02:50:19.147 回答
0

${requestScope.action}在你的jsp页面中使用。如果它返回错误值,则public String getAction()在您的操作中声明一个 getter 方法 () 并使用 previus EL 代码获取结果

action value: ${requestScope.action}
于 2013-07-15T21:13:46.890 回答