struts2如何接收uri里的参数

zwllxs2 2008-06-26 11:49:06
我在一个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不熟悉,一时不知道从哪下手,困扰我太久了,还请大家帮忙看看
...全文
1002 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jy02411368 2008-09-11
  • 打赏
  • 举报
回复
我晕 我可以回复了饿。。。
l_y_008 2008-08-07
  • 打赏
  • 举报
回复
咋么搞定的???
jabeginner 2008-06-26
  • 打赏
  • 举报
回复
像这样应该是不会出错的吧!!
String str=request.getParameter("type");
zwllxs2 2008-06-26
  • 打赏
  • 举报
回复
连接奋战几个小时,终于搞定了

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧