struts2学习笔记
Posted AndroidJava
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2学习笔记相关的知识,希望对你有一定的参考价值。
一、Action类的编写方式
二、Action获取请求参数
方式一:通过request获取属性注入
private static final long serialVersionUID = 1L;
private String name; private Integer age;
public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public String list(){ //将属性值放入request域中 //相当于request的setAttribute ActionContext.getContext().put("name",name); ActionContext.getContext().put("age",age); return NONE; } |
浏览器访问:http://localhost:8080/Day02/param_regist?name=test&age=12
方式二:Model属性注入
private User user;
public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String regist(){ System.out.println(user); return NONE; } |
浏览器访问:http://localhost:8080/Day02/param_regist?user.name=test&user.age=12
方式三:ModelDriven注入需要实现ModelDriven接口
private User user=new User(); @Override public User getModel() { returnuser; } public String regist(){ System.out.println(user); returnNONE; } |
浏览器访问:http://localhost:8080/Day02/param_regist?name=test&age=12
三、案例
List数据封装
private List<User>users=new ArrayList<>();
public List<User> getUsers() { returnusers; } publicvoid setUsers(List<User> users) { this.users = users; } public String collect(){ for(User user:users){ System.out.println(user); } returnNONE; }
|
<form action="${pageContext.request.contextPath}/collection_collect"> <fieldset> <legend>信息一</legend> <label for="name">姓名</label> <input type="text" id="name" name="users[0].name"/> <label for="age">年龄</label> <input type="text" id="age" name="users[0].age"/>
<legend>信息一</legend> <label for="name">姓名</label> <input type="text" id="name" name="users[1].name"/> <label for="age">年龄</label> <input type="text" id="age" name="users[1].age"/> </fieldset> <input type="submit" value="提交"/> </form> |
Map数据封装
private Map<String,User>userMap=new HashMap<>();
public Map<String, User> getUserMap() { returnuserMap; } publicvoid setUserMap(Map<String, User> userMap) { this.userMap = userMap; } public String collect(){ Set<String>keies=userMap.keySet(); for(String key:keies){ System.out.println(userMap.get(key)); } returnNONE; } |
<form action="${pageContext.request.contextPath}/collection_collect"> <fieldset> <legend>信息一</legend> <label for="name">姓名</label> <input type="text" id="name" name="userMap['user1'].name"/> <label for="age">年龄</label> <input type="text" id="age" name="userMap['user1'].age"/>
<legend>信息一</legend> <label for="name">姓名</label> <input type="text" id="name" name="userMap['user2'].name"/> <label for="age">年龄</label> <input type="text" id="age" name="userMap['user2'].age"/> </fieldset> <input type="submit" value="提交"/> </form> |
四、类型转换器
自定义时间转换器案例
自定义MyDateConvert 转换器
publicclass MyDateConvert extends StrutsTypeConverter{ @Override public Object convertFromString(Map context, String[] values, Class toClass) { if(values!=null && values.length==1 && toClass.equals(Date.class)){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/hh"); try { returnsimpleDateFormat.parse(values[0]); } catch (ParseException e) { e.printStackTrace(); } } returnnull; }
@Override public String convertToString(Map context, Object o) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/hh"); returnsimpleDateFormat.format((Date)o); } } |
配置,注意配置文件必须在编译后的classes目录下
使用
publicclass ConvertController extends ActionSupport{ private String name; private Date birthday; publicvoid setName(String name) { this.name = name; } publicvoid setBirthday(Date birthday) { this.birthday = birthday; }
public String convert(){ System.out.println(name); System.out.println(birthday); returnNONE; } } |
五、Struts拦截器
执行流程图(详细)
执行流程分析
默认拦截器
LoginInterceptor
struts.xml
以上是关于struts2学习笔记的主要内容,如果未能解决你的问题,请参考以下文章