1. 程式人生 > >struts2前臺向後臺傳遞map

struts2前臺向後臺傳遞map

前臺jsp:

	<form action="topic_list" method="post">
   		<input type="text" name="hp.ttopic"/>
   		<br/>
   		<input type="text" name="hp.tstate"/>
   		<input type="submit"/>
   	</form>

後臺Action
package com.action;

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

import com.dao.hib.BbsSection;
import com.dao.hib.BbsSectionDAO;
import com.dao.hib.BbsTopic;
import com.dao.hib.BbsTopicDAO;
import com.dao.hib.BbsUsers;
import com.dao.hib.BbsUsersDAO;
import com.entity.BBSTopic;

/**
 * 帖子控制器
 * @author Administrator
 *
 */
public class BBSTopicAction extends BaseAction {
	private BbsTopic topic;
	private int uid;
	private int sid;
	/**接受Map引數*/
	private Map<String,String[]> hp ;
	

	/**
	 * 釋出帖子
	 * @return
	 */
	public String pub(){
		BbsUsers user = new BbsUsersDAO().findById(uid);
		BbsSection sec = new BbsSectionDAO().findById(sid);
		topic.setBbsUsers(user);
		topic.setBbsSection(sec);
		topic.setTtime(new Timestamp(System.currentTimeMillis()));
		BbsTopicDAO tDAO = new BbsTopicDAO();
		tDAO.save(topic);
		return SUCCESS;
	}
	/**
	 * 獲取所有帖子列表
	 * @return
	 */
	public String getAll(){
		BbsTopicDAO tDAO = new BbsTopicDAO();
		List<BBSTopic> all = tDAO.findAll();
		//將資料放入request
		request.setAttribute("allTopic", all);
		return "topicAll";
	}
	
	/**
	 * 分頁查詢帖子資料
	 * @return
	 */
	public String list(){
		//
		for(Map.Entry<String, String[]> entry:hp.entrySet()){
			System.out.println("key="+entry.getKey());
			System.out.println("value="+entry.getValue()[0]);
		}
		
		return "test";
	}
	
	
	
	public BbsTopic getTopic() {
		return topic;
	}
	public void setTopic(BbsTopic topic) {
		this.topic = topic;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public Map<String, String[]> getHp() {
		return hp;
	}
	public void setHp(Map<String, String[]> hp) {
		this.hp = hp;
	}
	
}