日期:2014-05-17  浏览次数:20854 次

struts2.0里面找不到session。求各位大侠救命啊
这是我的毕业设计,我和我的搭档是用HSS三大框架都用上了---------


-------------------所有的Action都继承这个类-----------------------------------------------
public class CommonAction extends ActionSupport implements SessionAware, ServletRequestAware ,ServletResponseAware{
private static final long serialVersionUID = 1L;
protected HttpServletRequest request;
protected Map<String, Object> session;
protected HttpServletResponse response;
/**
* 将信息存入context区域,页面取值可使用<s:property value="#key">显示

* @param key
* @param msg
*/
protected void setMessage(String key, String msg) {
System.out.println("aaaa");
ActionContext.getContext().put(key, msg);
}


public HttpServletRequest getRequest() {
return request;
}


public void setRequest(HttpServletRequest request) {
this.request = request;
}


public HttpServletResponse getResponse() {
return response;
}


public void setResponse(HttpServletResponse response) {
this.response = response;
}


public Map<String, Object> getSession() {
return session;
}


@Override
public void setSession(Map<String, Object> arg0) {
session = arg0;
System.out.println(session.hashCode());

}

public void setServletRequest(HttpServletRequest arg0) {
request = arg0;
}


@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response=response;
}

}

---------------------在LoginAction里面将user放进去-----------------------------------

public String login(){
try {
Users user1=(Users)userService.login(user.getRegName(),DegistUtil.md5(user.getRegPassword()));
if(user1==null){
System.out.println("用户名密码错误");
request.setAttribute("login_error", "用户名或密码不正确");
return "error";

}else{
System.out.println("登入成功");
session.put("login_user", user1);
System.out.println(session.hashCode()+"ccc");
return "success";
}
} catch (Exception e) {
// TODO Auto-generated catch block
request.setAttribute("login_error", "系统繁忙,请稍后再试");
e.printStackTrace();
return "error";
}


}
----------------------------------------------------
然后 我在JSP页面上  

 <s:property value="#session['login_user'].regUsername" />  
-----------------------------------------------------
struct.xml里面的文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<include file="struts-admin.xml" />
<include file="struts-image.xml" />
<include file="struts-main.xml" />
<include file="struts-user.xml"></include>
<constant name="strusts.ObjectFactory" value="spring" />
<constant name="struts.ObjectFactory.spring.autoWire" value="name" />

<package name="product-default" extends="struts-default,json-default">

</package>
</struts>  
其他xml文件都  

<package name="product-main" namespace="/main" extends="product-default">

------------------------