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;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值