请问高手,struts2中action中取不到session的值。
代码如下:
java代码如下:
public class LoginAction extends BaseAdminAction {
getRequest().getSession().setAttribute(SystemUtil.USERINFO, user);
ActionContext.getContext().getSession().put("dishesPM","dishesPM");
System.out.println("================LoginAction.checkLogin() USERINFO/dishesPM set=============="+getRequest().getSession().getId());
return ajax(Status.success, "登录成功");
}
public class IndexAction extends BaseAdminAction {
private static final long serialVersionUID = 12L;
public String index(){
String test = (String)getRequest().getSession().getAttribute("dishesPM");
//System.out.println(test+"===========IndexAction.list() dishesPM get================="+getRequest().getSession().getId());
test = (String)ActionContext.getContext().getSession().get("dishesPM");
System.out.println(test+"===========IndexAction.list() dishesPM get================="+getRequest().getSession().getId());
return "index";
}
}
基类写了几个公共的方法如下:
public class BaseAdminAction extends ActionSupport {// 获取Request
// 获取Request
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
// 获取Response
protected HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
// 获取ServletContext
protected ServletContext getServletContext() {
return ServletActionContext.getServletContext();
}
// 获取Attribute
protected Object getAttribute(String name) {
return ServletActionContext.getRequest().getAttribute(name);
}
// 设置Attribute
protected void setAttribute(String name, Object value) {
ServletActionContext.getRequest().setAttribute(name, value);
}
// 获取Parameter
protected String getParameter(String name) {
return ServletActionContext.getRequest().getParameter(name);
}
// 获取Parameter数组
protected String[] getParameterValues(String name) {
return ServletActionContext.getRequest().getParameterValues(name);
}
// 获取Session
protected Object getSession(String name) {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
return session.get(name);
}
// 设置Session
protected void setSession(String name, Object value) {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
session.put(name, value);
}
// 移除Session
protected void removeSession(String name) {
ActionContext actionContext = ActionContext.getContext();
Map<String, Object> session = actionContext.getSession();
session.remove(name);
}
}
struts.xml文件
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="xhtml"/> <!-- .xhtml为访问扩展名 -->
<constant name="struts.convention.action.name.separator" value="_" />
<constant name="struts.convention.result.path" value="/jsps/" />
<constant name="struts.convention.classes.reload" value="false" />
<constant name="struts.convention.action.packages" value="com.define.dishes.action" />
<constant name="struts.objectFactory" value="spring" />
<package name="basePackage" extends="struts-default">
<interceptors>
<interceptor-stack name="baseStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="checkbox" />
<interceptor-ref name="multiselect" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="actionMappingParams" />
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,.*\\u0023.*,.*\\x5Cu0023.*,.*\\x5cu0023.*</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
<param name="validateAnnotatedMethodOnly">true</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="baseStack" />
<default-action-ref name="errorPage404Action" />
<action name="errorPage404Action">
<result type="redirect">/commons/error_page_404.html</result>
</action>
</package>
<package name="admin" extends="basePackage" namespace="/admin">
<interceptors>
<interceptor name="logInterceptor" class="com.define.dishes.interceptor.LoginedCheckInterceptor" />
<interceptor name="trimInterceptor" class="com.define.dishes.interceptor.TrimInterceptor" />
<interceptor-stack name="adminStack">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/pjpeg,image/gif,image/bmp,image/x-ms-bmp,image/png,image/x-png</param>
<param name="maximumSize">2097152</param>
</interceptor-ref>
<interceptor-ref name="baseStack" />
<interceptor-ref name="logInterceptor" />
<interceptor-ref name="trimInterceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="adminStack" />
<global-results>
<result name="main" >/jsps/admin/main.jsp</result>
<result name="login" >/jsps/admin/login.jsp</result>
<result name="index" >/jsps/admin/index.jsp</result>
<result name="top" >/jsps/admin/top.jsp</result>
<result name="left" >/jsps/admin/left.jsp</result>
</global-results>
</package>
以上是主要的代码。
现在loginaction 转到的main.jsp可以取到session的值,但main.jsp进入到indexAction.action中就取不到session的值。所有的action类都一样,经过页面进来的action都不能取到session的值。
相关推荐
从开发者角度看,就是说需要显示给用户的数据可以直接从Action中获取,而不像Struts1.x那样,必须把相应的Bean存到Page、Request或者Session中才能获取。Struts1.x 必须继承org.apache.struts.action.Action或者其...
而不像Struts1.x那样,必须把相应的Bean存到Page、Request或者Session中才能获取。 Struts1.x 必须继承org.apache.struts.action.Action或者其子类,表单数据封装在FormBean中。 Struts 2无须继承任何类型或实现...
action: 是struts中action层 主要处理表现层的东西 facade:只是一个门面模式(不起多大的作用)主要是处理耦合性问题 session:处理业务逻辑的 dao:数据控制层 调用过程是:action -->facade-->session-->dao ...
将用户信息保存到Session中。 Map, Object> session = ActionContext.getContext().getSession(); session.put("userName", userName); session.put("passWord", passWord); // 2. taglib标识传递 resultMsg =...
说当前版本因为原来使用find方法是从代理中取.而spring默认的数据持久话只能包含业务层和数据层.不能包含界面层.所以用find方法会导致session失效.故用get方法.现在这个问题已经解决.其实就是把原web.xml中的最后的...
充满了其他地方找不到的技术细节,而且深入剖析了Struts的内部机理。强烈推荐” ——JosephBrutto,资深程序员 作者简介 作者:(加拿大)Budi Kurniawan 译者:杨涛 王建桥 杨晓云 Budi Kurniawan,世界知名的...
2:继承:子类继承父类,子类可以拥有父类中已定义的方法,并且子类可以修改父类中的方法使其更适合特殊需求。 3:多台:不同对象对统一消息作出不同响应称之为多态 4:抽象:忽略与当前主题无关的那些方面,将...
控制层:Struts2控制器组件、Action组件 c.业务层:Bean组件 d.数据访问层:DAO组件(JDBC) 4.数据库设计 1)数据库导入 create database dangdang; //创建库 use dangdang; //进入dangdang库 set names utf8;...
action:存放struts2控制类的包 dao:数据库访问封装 enm:系统中使用到的常量包,这里不是用的常量,用的是枚举替代常量 entity:hibernate对应的orm与数据库表一一对应的实体类 filter:Log4jFormatFilter(格式化...
4.5.3 <jsp:setProperty>获取不到表单中的所有信息 4.6 精彩回顾 第5章 Web应用的缔造者 ——Servlet技术 5.1 本章学习任务 5.1.1 本章知识体系 5.1.2 实例开发任务 5.2 与时俱进——了解Servlet技术 5.2.1 初步...
• Before passing it to the Action class, Struts will also conduct form state validation by calling the validation() method on UserActionForm. Note: This is not always wise to do. There might be ways ...
并被填入表单相应的数据,ActionServler根据Struts-config.xml文件配置好的设置决定是否需要表单验证,如果需要就调用ActionForm的 Validate()验证后选择将请求发送到哪个Action,如果Action不存在,ActionServlet...
Struts ActionServlet控制器对象 642 Struts Action Classes 642 搞定Action对象 643 处理异常 643 Action的分类 643 Struts Action Mapping 646 使用ActionForward导航 647 Struts ActionForm Bean捕获表单数据 648...
LobHandler必须注入到Hibernate会话工厂sessionFactory中,因为sessionFactory负责产生与数据库交互的Session。LobHandler的配置如代码 5所示: 代码 5 Lob字段的处理句柄配置 1. 2. … 3. 4. class="org....
spring 默认注入为单例模式 如有些action不需要刻意在bean标签加入scope="prototype"属性 内部私有属性定义时最好赋初值(尤其字符串)这样对后面判断有益 如果action无法传值 刻意考虑用session传值 2013-08-29 ...
* 定义需求:部门信息的页面数据要插入到数据库中 * 定义表 CREATE TABLE `sys_user_group` ( `id` INTEGER(11) NOT NULL AUTO_INCREMENT, #编号 `remark` TEXT, #备注 `name` VARCHAR(100) ...
2.Struts action向导:根据jsp文件中的表单内容自动生成的form类和action类的代码. 并修改struts-config.xml配置文件. 这个FirstJava2开发工具编辑辅助方式比Eclipse和Jbuild更高效,使简单, 自己使用Eclipse和Jbuild...
使用import指令导入库到模板中,Freemarker会为导入的库创建新的名字空间,并可以通过import指令中指定的散列变量访问库中的变量: ${my.mail} ${mail} 输出结果: <p>Copyright (C) 1999-2002 ...