Struts2获取request三种方法
struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象。
在Action中获取request方法一:
在Action中的代码:
Map request =(Map)ActionContext.getContext().get("request");
List<Task> tasks =taskManager.findAll();
request.put("tasks", tasks);
在JSP页面中获取其中的值:
<s:iterator id="task"value="#request.tasks">
</s:iterator>
--------------------------------------------------------------------------------------------
方法二:通过ServletActionContext类来获取,使用struts2经验如果处理get传参是中文,只能使用该方法进行处理乱码问题
Action中代码:
HttpServletRequest request =ServletActionContext.getRequest();
request.setAttribute("username", "zhangsan");
在jsp中获取其中的值
--------------------------------------------------------------------------------------------
方法三:通过ServletRequestAware接口通过IOC机制注入Request对象
Action中的代码:
Action实现ServletRequestAware接口,实现接口中的方法
在jsp页面中获取其中的值
<s:propertyvalue="#request.task.tname"/>