struts2 action 调用构造方法问题

SambaGao 2010-02-07 11:20:30

private String name ;
private Map request ;
private Map session ;

public DMLAction() {
request = (Map)ActionContext.getContext().get("request") ;
}

public String execute() {
System.out.println("name=" + name) ;
request.put("s1","gjhohj") ;
return SUCCESS ;
}



<package name="default" namespace="/t" extends="struts-default">

<action name="user" class="com.xlh.gjhohj.DMLAction">
<result>
/hello.jsp
</result>
</action>
</package>


当从 struts.xml找到DMLAction这个类时应该调用DMLAction的构造方法才对。可是为什么没有调用呢?

...全文
226 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanweijie0317 2010-06-12
  • 打赏
  • 举报
回复
楼主的说法有问题,struts2.0和struts2.1都执行new Action的构造方法
老张-AI 2010-02-07
  • 打赏
  • 举报
回复
ActionContext.getContext().put("list",list);
蛋黄车 2010-02-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 microscale 的回复:]
要取得request对象 可以通过:

HttpServletRequest r = ServletActionContext.getRequest()  ;
[/Quote]

这才是在struts2的action中获取request对象的方法吧

LZ获取request的方法没有见过
xinzhifan4 2010-02-07
  • 打赏
  • 举报
回复
确实没见过这种用法 ,楼上的兄弟能介绍下,在什么情况下会用到这种用法?谢谢
SambaGao 2010-02-07
  • 打赏
  • 举报
回复
报 request null
request = (Map)ActionContext.getContext().get("request") ;

放到 execute就没问题
microscale 2010-02-07
  • 打赏
  • 举报
回复
要取得request对象 可以通过:

HttpServletRequest r = ServletActionContext.getRequest() ;
microscale 2010-02-07
  • 打赏
  • 举报
回复
怎么确定就没有调用?
sotom 2010-02-07
  • 打赏
  • 举报
回复
 public DMLAction() {
request = (Map)ActionContext.getContext().get("request") ;
}


第一次见这么用,汗啊,原来我很山寨,很菜。
SambaGao 2010-02-07
  • 打赏
  • 举报
回复
已经解决了。


<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

<!-- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> -->


原来用的是struts2.0 居然没有执行new Action的构造方法。改成struts2.1以后就没事了。

81,122

社区成员

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

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