<%@ 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>
相关推荐
3. **值栈维护**:如果验证失败并需要返回到输入页面时,为了避免值栈中的数据丢失,可以实现`Preparable`接口并在`prepare()`方法中初始化需要的值。同时,需要在Action配置中添加`...
- 如果验证失败后返回到`input`页面,存储在`ValueStack`中的值可能会丢失。解决办法是让Action实现`Preparable`接口,并在`prepare()`方法中重新加载或初始化这些值。 - 若使用`Preparable`接口,则必须在`action`...
5. **处理验证失败**:当验证失败时,Struts2会自动将错误信息放入ValueStack,然后跳转到`input`属性指定的页面。但由于ValueStack中的值可能会丢失,你可以选择实现`Preparable`接口并在`prepare()`方法中重新初始...
- 使用`token`拦截器时,如果客户端的Token丢失,可能会导致“令牌不匹配”错误,这时需要正确处理这种情况,比如提示用户重新提交。 - 考虑到并发问题,确保在多线程环境下正确处理Token。 综上所述,Struts框架...
- 使用正则表达式验证日期格式是否正确。 - 将字符串转换为Date对象进行比较。 - 示例代码: ```javascript function isValidDate(dateStr) { const dateRegex = /^\d{4}-\d{2}-\d{2}$/; if (!dateRegex.test...