add.jsp: <body> <s:form action="AddUserAction"> <s:textfield name="user.userName" label="Your name" /> <s:textfield name="user.address.address" label="Your address" /> <s:textfield name="user.address.zipCode" label="Your zipCode" /> <s:textfield name="user.address.phone" label="Your phone" /> <s:textfield name="array" label="不使用下标的数组" /> <s:textfield name="array" label="不使用下标的数组" /> <s:textfield name="initArray[0]" label="使用下标的数组" /> <s:textfield name="initArray[1]" label="使用下标的数组" /> <s:textfield name="list[0]" label="List" /> <s:textfield name="list[1]" label="List" /> <s:textfield name="map['user1'].userName" label="Map" /> <s:textfield name="map.user2.userName" label="Map" /> <s:submit/> </s:form> </body>
success.jsp: <body> <h4>User: <s:property value="user.userName" /> add success.</h4> <h4>Address:<s:property value="user.address.address" /></h4> <h4>zipCode:<s:property value="user.address.zipCode" /></h4> <h4>phone :<s:property value="user.address.phone" /></h4> <!-- set 将对象设置为ActionContext中的属性映射,默认作用域是Action --> <s:set name="username" scope="session" value="user.userName" /> <h4>Hello <s:property value="#session['username']" /></h4> <!-- push 将ActionContext中的对象放入ValueStack顶端,方便标签内访问。 --> <s:push value="user"> <h4>Show userName(<s:property value="userName" />) by push user in ValueStack.</h4> </s:push> <!-- bean 创建一个类的实例,默认放置在ValueStack中,标签外访问需要设置var为实例指定一个引用,该引用会作为命名参数存在于ActionContext --> <s:bean name="com.jaeson.hibernatestudy.bean.User" var="bean"> <s:param name="userName">jaesonchen</s:param> <h4>User bean for User.getUserName()=<s:property value="getUserName()" /></h4> </s:bean> <s:if test="#bean.userName == 'jaesonchen1'"> <h4>if show #bean.userName=<s:property value="#bean.userName" /></h4> </s:if> <s:elseif test="user.address.zipCode == 100086"> <h4>elseif show user.zipCode=<s:property value="user.address.zipCode" /></h4> </s:elseif> <s:else> <h4>else show nothing</h4> </s:else> <!-- iterator 遍历Collection Map Enum Iterator Array,支持在ActionContect中定义一个保存遍历状态的变量 --> <s:iterator value="array" status="itStatus"> <li> <s:property value="#itStatus.count" /> <s:property value="array[#itStatus.index]" /> </s:iterator> <s:iterator value="list" status="itStatus"> <li> <s:property value="#itStatus.count" /> <s:property /> </s:iterator> <s:iterator value="map" status="itStatus"> <li> <s:property value="key" /> <s:property value="value.userName" /> </s:iterator> <!-- Map遍历的类型为Entry(key, value),默认情况下listKey指向key,listValue指向value --> <s:select name="selectList" list="{'chenzq','jaeson','jaesonchen'}" label="select a name"/> <s:select name="selectList" list="list" label="select a num"/> <s:select name="selectMap" list="map" listValue="value.userName" label="select a username"/> <s:radio name="radioList" list="{'chenzq','jaeson','jaesonchen'}" label="radio a name" /> </body>
import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.context.annotation.Scope; import com.opensymphony.xwork2.ActionSupport; import com.jaeson.hibernatestudy.bean.User; import com.jaeson.springstudy.service.CommonService; @SuppressWarnings("all") @Scope("prototype") @Controller("userAction") public class UserAction extends ActionSupport { @Override public String execute() { this.getCommonService().saveOrUpdate(this.getUser()); //throw new RuntimeException("throw in UserAction"); return SUCCESS; } @Override public void validate() { if (this.getUser().getUserName().length() == 0) { this.addFieldError("user.userName", "userName is required."); } } /** * 使用相同名字array提交时,不需要初始化数组。 * 使用initArray[0]提交时,必须初始化数组,否则初始化错误 ognl.OgnlException: target is null for setProperty。 * List Map等集合类型时,两种提交方式都不能初始化集合。 * 在类型转换错误需要返回input时,使用无下标提交的集合每一个输入框都会显示全部的集合内容,最佳实践使用集合下标。 */ private String[] array; private double[] initArray = new double[10]; private List<Integer> list; private Map<String, User> map; private User user; @Resource(name = "commonService") private CommonService commonService; public User getUser() { return this.user; } public void setUser(User user) { this.user = user; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public double[] getInitArray() { return initArray; } public void setInitArray(double[] initArray) { this.initArray = initArray; } public List<Integer> getList() { return list; } public void setList(List<Integer> list) { this.list = list; } public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } public CommonService getCommonService() { return this.commonService; } public void setCommonService(CommonService commonService) { this.commonService = commonService; } }
struts.xml:
<action name="AddUserAction" class="userAction">
<result name="success">/success.jsp</result>
<result name="input">/add.jsp</result>
</action>
import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.context.annotation.Scope; import com.opensymphony.xwork2.ActionSupport; import com.jaeson.hibernatestudy.bean.User; import com.jaeson.springstudy.service.CommonService; @SuppressWarnings("all") @Scope("prototype") @Controller("userAction") public class UserAction extends ActionSupport { @Override public String execute() { this.getCommonService().saveOrUpdate(this.getUser()); //throw new RuntimeException("throw in UserAction"); return SUCCESS; } @Override public void validate() { if (this.getUser().getUserName().length() == 0) { this.addFieldError("user.userName", "userName is required."); } } /** * 使用相同名字array提交时,不需要初始化数组。 * 使用initArray[0]提交时,必须初始化数组,否则初始化错误 ognl.OgnlException: target is null for setProperty。 * List Map等集合类型时,两种提交方式都不能初始化集合。 * 在类型转换错误需要返回input时,使用无下标提交的集合每一个输入框都会显示全部的集合内容,最佳实践使用集合下标。 */ private String[] array; private double[] initArray = new double[10]; private List<Integer> list; private Map<String, User> map; private User user; @Resource(name = "commonService") private CommonService commonService; public User getUser() { return this.user; } public void setUser(User user) { this.user = user; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public double[] getInitArray() { return initArray; } public void setInitArray(double[] initArray) { this.initArray = initArray; } public List<Integer> getList() { return list; } public void setList(List<Integer> list) { this.list = list; } public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } public CommonService getCommonService() { return this.commonService; } public void setCommonService(CommonService commonService) { this.commonService = commonService; } }
struts.xml:
<action name="AddUserAction" class="userAction">
<result name="success">/success.jsp</result>
<result name="input">/add.jsp</result>
</action>