Struts2.3接收post方式提交的表单参数的方式

一:方式一:通过request来获取,首先让action实现ServletRequestAware接口,然后通过request来获取提交的参数,代码如下:

package cn.gov.csrc.flight.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.gov.csrc.base.action.BaseAction;
import cn.gov.csrc.cms.model.UserInfo;
import cn.gov.csrc.cms.service.UserService;
import cn.gov.csrc.flight.model.Flgtinfo_D;
import cn.gov.csrc.flight.service.FlgtinfodService;

@Controller
@ParentPackage("json-default") 
@Scope("prototype")
public class FlgtinfodAction extends BaseAction implements ServletRequestAware{
	
	private static final long serialVersionUID = 1L;
	
	private FlgtinfodService flgtinfodService;
	
	private Map dataMap;
	
	HttpServletRequest request;
	
	public FlgtinfodAction(){
		dataMap = new HashMap();
	}
	
	@Resource
	public void setFlgtinfodService(FlgtinfodService flgtinfodService) {
		this.flgtinfodService = flgtinfodService;
	}
	
	@Action(value = "FlgtinfodAction_detail", results = { @Result(name = "success", type = "json", params = {"root", "dataMap" }) })
	public String detail(){
		dataMap.clear(); 
		//查询出港详情(根据用户id,列表到详情的唯一编号)
		String id = this.request.getParameter("id");
		Flgtinfo_D flgtinfo_D = flgtinfodService.getEntity(id);
		String airline2 = flgtinfo_D.getAirline();
		// 出港详情放入到dataMap中
		dataMap.put("flgtinfo_D", flgtinfo_D);
		// 放入一个是否操作成功的标识 
		dataMap.put("success", true);
		return "success";
	}

	public Map getDataMap() {
		return dataMap;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
}

二:方式二,直接在action里面写你要提交的参数,然后提供set,get方法,代码如下:

package cn.gov.csrc.flight.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.gov.csrc.base.action.BaseAction;
import cn.gov.csrc.cms.model.UserInfo;
import cn.gov.csrc.cms.service.UserService;
import cn.gov.csrc.flight.model.Flgtinfo_D;
import cn.gov.csrc.flight.service.FlgtinfodService;

@Controller
@ParentPackage("json-default") 
@Scope("prototype")
public class FlgtinfodAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private Integer id; // 列表到详情的唯一标识
	
	private FlgtinfodService flgtinfodService;
	
	private Map dataMap;
	
	public FlgtinfodAction(){
		dataMap = new HashMap();
	}
	
	@Resource
	public void setFlgtinfodService(FlgtinfodService flgtinfodService) {
		this.flgtinfodService = flgtinfodService;
	}
	
	@Action(value = "FlgtinfodAction_find", results = { @Result(name = "success", type = "json", params = {"root", "dataMap" }) })
	public String find() {
        dataMap.clear(); 
		//查询出港信息
		List list = flgtinfodService.findAllFlgtinFods();
		// 出港信息放入到dataMap中
		dataMap.put("list", list);
		// 放入一个是否操作成功的标识 
		dataMap.put("success", true);
		return "success";
	}
	
	@Action(value = "FlgtinfodAction_detail", results = { @Result(name = "success", type = "json", params = {"root", "dataMap" }) })
	public String detail(){
		dataMap.clear(); 
		//查询出港详情(根据用户id,列表到详情的唯一编号)
		id = model.getId();
		Flgtinfo_D flgtinfo_D = flgtinfodService.getEntity(id);
		String airline2 = flgtinfo_D.getAirline();
		// 出港详情放入到dataMap中
    		dataMap.put("flgtinfo_D", flgtinfo_D);
    		// 放入一个是否操作成功的标识 
    		dataMap.put("success", true);
		return "success";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Map getDataMap() {
		return dataMap;
	}
	
	
}



你可能感兴趣的:(Struts2.3接收post方式提交的表单参数的方式)