1. 程式人生 > >Struts2.3接收post方式提交的表單引數的方式

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<Flgtinfo_D> implements ServletRequestAware{
	
	private static final long serialVersionUID = 1L;
	
	private FlgtinfodService flgtinfodService;
	
	private Map<String, Object> dataMap;
	
	HttpServletRequest request;
	
	public FlgtinfodAction(){
		dataMap = new HashMap<String, Object>();
	}
	
	@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<String, Object> 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<Flgtinfo_D>{
	
	private static final long serialVersionUID = 1L;
	
	private Integer id; // 列表到詳情的唯一標識
	
	private FlgtinfodService flgtinfodService;
	
	private Map<String, Object> dataMap;
	
	public FlgtinfodAction(){
		dataMap = new HashMap<String, Object>();
	}
	
	@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<Flgtinfo_D> 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<String, Object> getDataMap() {
		return dataMap;
	}
	
	
}