<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'prepare.jsp' starting page</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <s:form action="PrepareAction"> <s:textfield name="id" label="Your id" /> <s:select name="name" list="#request.list" label="select a name"/> <s:submit/> </s:form> </body> </html>
import java.util.List; import java.util.Arrays; import java.util.Map; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.Preparable; import org.apache.struts2.interceptor.RequestAware; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; @SuppressWarnings("all") @Scope("prototype") @Controller("prepareAction") public class PrepareAction extends ActionSupport implements Preparable, RequestAware { @Override public String execute() { System.out.println("in execute... id = " + this.getId() + ", name = " + this.getName()); return SUCCESS; } @Override public void validate() { if (this.getId() == null || this.getId().length() == 0) { this.addFieldError("id", "Id is required."); } System.out.println("in validate..."); } @Override public void prepare() throws Exception { List<String> list = Arrays.asList("chenzq", "jaeson", "czq"); this.getRequest().put("list", list); System.out.println("in prepare..."); } private Map<String, Object> request; private String id; private String name; public Map<String, Object> getRequest() { return request; } public void setRequest(Map<String, Object> request) { this.request = request; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
在struts.xml中的配置:
<!-- prepare 验证失败返回列表信息丢失,使用prepare每次提交时将list数据放入request中 -->
<action name="PrepareAction" class="prepareAction">
<result name="success">/welcome.jsp</result>
<result name="input">/prepare.jsp</result>
</action>
import java.util.List; import java.util.Arrays; import java.util.Map; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.Preparable; import org.apache.struts2.interceptor.RequestAware; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; @SuppressWarnings("all") @Scope("prototype") @Controller("prepareAction") public class PrepareAction extends ActionSupport implements Preparable, RequestAware { @Override public String execute() { System.out.println("in execute... id = " + this.getId() + ", name = " + this.getName()); return SUCCESS; } @Override public void validate() { if (this.getId() == null || this.getId().length() == 0) { this.addFieldError("id", "Id is required."); } System.out.println("in validate..."); } @Override public void prepare() throws Exception { List<String> list = Arrays.asList("chenzq", "jaeson", "czq"); this.getRequest().put("list", list); System.out.println("in prepare..."); } private Map<String, Object> request; private String id; private String name; public Map<String, Object> getRequest() { return request; } public void setRequest(Map<String, Object> request) { this.request = request; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
在struts.xml中的配置:
<!-- prepare 验证失败返回列表信息丢失,使用prepare每次提交时将list数据放入request中 -->
<action name="PrepareAction" class="prepareAction">
<result name="success">/welcome.jsp</result>
<result name="input">/prepare.jsp</result>
</action>