struts2有内建的类型转换器,boolean和Boolean,char和Character,int和Integer,long和Long,float和Float,double和Double,Date,字符串类型数组,String类型集合。
基于OGNL的类型转换。
struts2.xml
这样就能自动组装User对象了。
还能对Map对象进行自动组装。
Action:
对于List类型的话用索引即可,如<s:textfield name="users[0].username"></s:textfield>
若不使用范型,则要配置一个资源文件,文件名命名规则:ActionName-conversion.properties,并将此文件与action类文件放在同一个包下。例如:LoginMapAction-conversion.properties,内容是:Key_users=java.lang.String
Element_users=com.randy.action.User
:arrow: 交流群:132607763 若满了,请加:251207741
基于OGNL的类型转换。
package com.randy.com.entity;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
struts2.xml
<action name="loginUser" class="com.randy.com.LoginUserAction">
<result name="success">/succ.jsp</result>
</action>
package com.randy.com;
import com.opensymphony.xwork2.ActionSupport;
import com.randy.com.entity.User;
public class LoginUserAction extends ActionSupport {
private User user;
public String execute() {
System.out.println(user.getUsername() + ":" + user.getPassword());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>user login</title>
</head>
<body>
<s:form action="loginUser.action">
<s:textfield name="user.username" label="name"></s:textfield>
<s:password name="user.password" label="password"></s:password>
<s:submit value="submitttt"></s:submit>
</s:form>
</body>
</html>
这样就能自动组装User对象了。
还能对Map对象进行自动组装。
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="loginMap.action">
<s:textfield name="users['one'].username"></s:textfield>
<s:password name="users['one'].password"></s:password>
<s:textfield name="users['me'].username"></s:textfield>
<s:password name="users['me'].password"></s:password>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
Action:
package com.randy.action;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport;
import com.randy.entity.User;
public class LoginMapAction extends ActionSupport {
private static final long serialVersionUID = 9143681290982374696L;
private Map<String, User> users;
public String execute() {
System.out.println(users.size());
Set<String> keySet = users.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + ":" + users.get(key).getUsername() + "_"
+ users.get(key).getPassword());
}
return SUCCESS;
}
public Map<String, User> getUsers() {
return users;
}
public void setUsers(Map<String, User> users) {
this.users = users;
}
}
<s:text name="succTip">
<s:param>${users.get('me').username}</s:param>
</s:text>
对于List类型的话用索引即可,如<s:textfield name="users[0].username"></s:textfield>
若不使用范型,则要配置一个资源文件,文件名命名规则:ActionName-conversion.properties,并将此文件与action类文件放在同一个包下。例如:LoginMapAction-conversion.properties,内容是:Key_users=java.lang.String
Element_users=com.randy.action.User
:arrow: 交流群:132607763 若满了,请加:251207741