问题陈述
我想在 struts 操作重定向到 JSP 页面时发送一些额外的参数。如果它是一个或两个较小的参数,那么我可以以查询参数的形式传递它们,即Request.jsp?param1=hello
.
我试过的
我检查了这个网站并做了相应的事情。这是我的struts.xml
文件:
<struts>
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.maxSize" value="100000000" />
<!-- <constant name="struts.action.excludePattern" value="/service" /> -->
<package name="default" extends="struts-default,json-default"
namespace="/">
<action name="request"
class="com.ModifyRequest">
<result name="success" type="redirect">Request.jsp?orderId=123
</result>
<result name="failure" type="redirect">Request.jsp?orderId=123
<param name="failureMessage">${failureMessage}</param>
<param name="status">${status}</param>
</result>
</action>
</package>
</struts>
这是我的动作课:
public class ModifyRequest extends ActionSupport implements Parameterizable{
private Map<String, String> params;
private String failureMessage;
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getFailureMessage() {
return failureMessage;
}
public void setFailureMessage(String failureMessage) {
this.failureMessage = failureMessage;
}
@Override
public void addParam(String arg0, String arg1) {
// TODO Auto-generated method stub
//params.put(arg0, arg1);
}
@Override
public Map<String, String> getParams() {
// TODO Auto-generated method stub
return params;
}
@Override
public void setParams(Map<String, String> params) {
// TODO Auto-generated method stub
Map<String,String> paramMap = new HashMap<String, String>();
paramMap.put("failureMessage", "failureMessage"); // temporary value
paramMap.put("status", "status"); // temporary value
this.params = paramMap;
}
@Override
public String execute() throws Exception {
if(false){
this.status = "success..";
this.failureMessage = "success message";
return SUCCESS;
} else{
this.status = "failure";
this.failureMessage = "failure message..";
return "failure";
}
}
}
我的 JSP 文件的相关部分:
<script type="text/javascript">
alert('<%=request.getParameter("failureMessage")%>');
alert('<%=request.getParameter("status")%>');
/*
// i have written an hidden element setting this value, so i get the desired output.
alert('<%=request.getParameter("userId")%>');
*/
<form action="request" method="post">
<input type="hidden" name="userId" id="userId" value= "joshi">
</form>
我没有在 Action 类中提到 userId 的 getter 和 setter,因为它不是必需的,所以请忽略这部分。
它在浏览器中引发以下异常:
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
File: org/apache/struts2/dispatcher/ServletRedirectResult.java
Line number: 257
Stacktraces
java.lang.NullPointerException
org.apache.struts2.dispatcher.ServletRedirectResult.isPathUrl
(ServletRedirectResult.java:257)
org.apache.struts2.dispatcher.ServletRedirectResult.doExecute(ServletRedirectResult.java:177)
如果我<param> elemnets
从result
元素中删除,那么它不会抛出任何异常,但我的问题没有解决。
你能帮我么。