Struts 工作笔记

struts2 会默认执行(在没有配置method 方法的时候调用的)方法。 @Override 


@Override
public String execute() throws Exception {

// getRequest().getSession().setAttribute("ecid", "87654321");

if(null != getRequest().getSession().getAttribute("ecid")
   && !"".equals(getRequest().getSession().getAttribute("ecid")))
{
if ( this.isEcIDUserExist()    ) { // before click the addButton ,check whether the
return "msg";
}
}
return super.execute();
}


=================================  在Strut2 中set 和get session 的方法
getRequest().getSession().setAttribute("ecid", "87654321");




=======================struts2 标签,获取获取,
如果在调用页面的时候,页面的下拉列表数据 是从 数据库中获取,可以采用 getXXXX 方法,那么struts2 action 会自动执行该方法。
案例:


package com.langteng.action;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.langteng.dao.BaseDao;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport{

private static final long serialVersionUID = 7291646010157096067L;
public List getAreaInfo() throws Exception {

StringBuffer sb = new StringBuffer();
sb.append(" select * from area_info ");

List<Object> list = new ArrayList<Object>();
List query = BaseDao.query(sb.toString());
if (null != query && query.size() > 0) {
for (int i = 0; i < query.size(); i++) {
Map map = (Map) query.get(i);
list.add(map);
}
}
return list;
}

public Map<String,String> getGenderList()
{
Map<String,String> map = new HashMap<String,String>();

map.put("1", "男");
map.put("0", "女");
return map;
}

public Map<String,String> getCan_used_List()
{
Map<String,String> map = new HashMap<String,String>();

map.put("1", "启用");
map.put("0", "停用");
return map;
}


public Map<String,String> getUser_levelList()
{
Map<String,String> map = new HashMap<String,String>();
map.put("2", "普通用户");
map.put("1", "高级用户");
return map;
}



/**
* get user author
* @return
* @throws Exception
*/
public Map<String,String> getUserAuther() throws Exception {

Map<String,String> mapSend = new HashMap<String,String>();
StringBuffer sb = new StringBuffer();

sb.append("   select * from `user_auth`  where auth_id != '1'");

List query = BaseDao.query(sb.toString() );
if (null != query && query.size() > 0) {

for (int i = 0; i < query.size(); i++) {
Map map = (Map) query.get(i);
mapSend.put( map.get("auth_id").toString(),  map.get("auth_name").toString());
}
}

System.out.println("map of mapSend.........." + mapSend );
return mapSend;
}


public String getEcId(){
if(null != getRequest().getSession().getAttribute("ecid") && !"".equals(getRequest().getSession().getAttribute("ecid"))){
return getRequest().getSession().getAttribute("ecid").toString();
}
return null;
}

public static HttpServletRequest getRequest(){
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
return request;
}
public static HttpServletResponse getResponse(){
ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
return response;
}

}

你可能感兴趣的:(DAO,apache,工作,struts,servlet)