struts2遇到的问题

来源:互联网 发布:索尼和夏普电视 知乎 编辑:程序博客网 时间:2024/04/29 11:30

1、在建立拦截器的时候,对用户名进行验证,开始是是这样

ActionContext act = invocation.getInvocationContext();Map session =act.getSession();String user = (String) session.get("username");


但发现,user的返回值一直为空

然后,在Action中加入

ActionContext.getContext().getSession().put("username", username);


这样,每次user都能返回上一个用户的名字,与自己的期望并不一样,经过各种查找,才明白,下面这种方式才能获取jsp中的数据

ActionContext act = invocation.getInvocationContext();Map<String, Object> session = act.getParameters();String[] user = (String[]) session.get("username");System.out.println(user[0]);

2、需要在web.xml中将/*配置为*.action时需要一些地方需要注意

1>首先web.xml中的对比

*action的配置

  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>*.action</url-pattern>  </filter-mapping>

/*的配置

  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>

2>jsp中的对比

*action中的写法

<form action="caoni.action">

/*中的写法

<form action="caoni">

3>如果web.xml中的配置为*.action时jsp中不能使用struts2的标签

必须在web中配置如下代码

 <filter-mapping>   <filter-name>struts2</filter-name>   <url-pattern>*.jsp</url-pattern>   </filter-mapping>


 


 


 


 

 

0 0
原创粉丝点击