使用prepare解决验证失败返回input时的列表数据丢失

<%@ 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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值