struts2如何接收uri里的参数
我在一个jsp页面里,用如下方式向action里传递参数:
<script type="text/javascript">
function openIndex()
{
alert("打开首页");
window.location.href="index.action?type=showindex"; //传递一个type参数
}
</script>
</head>
<body onload="openIndex()">
但是,.我如何才能在对应的action里获取到这个参数值呢?
我在对应的action里是按如下写的:
public class Produce extends ActionSupport
{
private String type;
//在struts2中获取request,它不同于struts1中的方式
HttpServletRequest request = ServletActionContext.getRequest();
public Produce()
{
System.out.println("produce调用");
// String str=request.getParameter("type");
// System.out.println("接收到参数: "+str);
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
只要我不注释String str=request.getParameter("type"); 就会报错,
错误信息如下:
javax.servlet.ServletException: Unable to instantiate Action, com.zwl.action.Produce, defined for 'index' in namespace '/'null
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
root cause
Unable to instantiate Action, com.zwl.action.Produce, defined for 'index' in namespace '/'null - action - file:/D:/zhangweilin/tools/about%20program/Tomcat-5.5.12/Tomcat-5.5.12/apache-tomcat-5.5.12/webapps/hongweinet/WEB-INF/classes/struts.xml:16:57
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:294)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:365)
com.opensymphony.xwork2.DefaultActionInvocation.access$000(DefaultActionInvocation.java:38)
com.opensymphony.xwork2.DefaultActionInvocation$1.doProfiling(DefaultActionInvocation.java:83)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.<init>(DefaultActionInvocation.java:74)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:41)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:494)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.12 logs.
我对struts2不熟悉,一时不知道从哪下手,困扰我太久了,还请大家帮忙看看