问题陈述
我想在struts action重定向到JSP页面时发送一些额外的参数。如果它是一个或两个尺寸较小的参数,那么我可以以查询参数的形式传递它们,即Request.jsp?param1=hello
。
我尝试了什么
我检查了this site并做了相应的工作。
这是我的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>
这是我的Action类:
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
,那么它不会抛出任何异常,但我的问题仍未解决。
答案 0 :(得分:1)
它应该更简单:在Struts2中,参数(无论是来自Querystring还是Form)都是通过Getters和Setters处理的。你没有发布你的JSP,所以我不知道你是否在做那些奇怪的事情,但其余部分:
Dispatcher Result
类型,而不是Redirect Result
类型; ActionMessage
和ActionError
将反馈从Action页面传送到JSP页面,由于这个原因,它们由框架提供; "success"
和"error"
,由ActionSupport
类(您扩展)公开为常量; Intelligent Default
,您可以省略"success"
结果的结果名称。struts.xml中
<action name="request" class="com.ModifyRequest">
<result>Request.jsp</result>
<result name="error">Request.jsp</result>
</action>
动作
public class ModifyRequest extends ActionSupport{
private String exampleParameterFromJSP;
public String getExampleParameterFromJSP() {
return exampleParameterFromJSP;
}
public void setExampleParameterFromJSP(String exampleParameterFromJSP) {
this.exampleParameterFromJSP = exampleParameterFromJSP;
}
@Override
public String execute() throws Exception {
if("1337".equals(exampleParameterFromJSP)){
addActionMessage("SUCCESS !! So long and thanks for all the fish!");
return SUCCESS;
} else{
addActionError("ERROR !! You must write '1337' to make it works!");
return ERROR;
}
}
}
Hypotetical JSP:
<s:actionerror />
<s:actionmessage />
<s:form action="request">
<s:textfield name="exampleParameterFromJSP" />
<s:submit value="I'm Feeling Lucky" />
</s:form>
发布JSP后,主问题现在很明显:
Scriptlet
s(<%
,<%=
... Old and Bad(tm),并在不同的范围内运行) ;
<form action="request" method="post">
<input type="hidden" name="userId" id="userId" value= "joshi">
</form>
应该是(首选解决方案):
<s:form action="request" method="post">
<s:hidden name="userId" />
</s:form>
或至少这个:
<form action="request" method="post">
<input type="hidden" name="userId" value="<s:property value='userId'/>" />
</form>
答案 1 :(得分:0)
这是另外添加@Andrea给出的答案,它工作正常。只是想提供一些额外的参考,以便更好地理解。
这是我的JSP(如问题所示):
<script type="text/javascript">
alert('<%=request.getParameter("failureMessage")%>');
alert('<%=request.getParameter("status")%>');
<form action="request" method="post">
<input type="hidden" name="userId" id="userId" value= "joshi">
</form>
使用request.getParameter()
返回作为查询参数发送的http请求参数,其中request.getAttribute()
返回服务器端设置的值,即servlet。
request.getParameter()
只返回字符串对象,其中request.getAttribute()
返回一个Object,它可以是任何类型。
该问题的另一种解决方案是在JSP中使用request.getAttribute()
来获取参数或proeprties值。无需在Action类中实现Parameterizable
接口,而只需提供属性及其getters
和setters
。
<script type="text/javascript">
alert('<%=request.getAttribute("failureMessage")%>');
alert('<%=request.getAttribute("status")%>');
<form action="request" method="post">
<input type="hidden" name="userId" id="userId" value= "joshi">
</form>
这是该问题的补充解决方案。