struts获取web页面时,request为null

struts获取web页面时,request为null。

错误信息如下:

java.lang.NullPointerException

    com.yy.action.loginAction1.execute(loginAction1.java:41)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)

action代码如下:

loginAction1.Java


package com.yy.action;


import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class loginAction1 extends ActionSupport{
	/*
	 * 
	 * */
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger("studyStruts2");


	private Map request;

	private Map session;

	private Map application;
	

	public loginAction1() {
		logger.info(new Throwable().getStackTrace()[0]);
		request = (Map)ActionContext.getContext().get("request");
		session = ActionContext.getContext().getSession();
		application = ActionContext.getContext().getApplication();
	}
	

	public String execute(){
		logger.info(new Throwable().getStackTrace()[0]);
		logger.info("a"+request);
		request.put("r1", "r2");
		application.put("a1", "a2");
		session.put("s1", "s2");
		return SUCCESS;
	}
}


一直找不到原因。看马士兵讲的视频时,说是web.xml中struts过滤器原因。果不其然,2.0的配置会使得获取的request为null,而2.1的配置正常。

web.xml中struts配置如下(红色为正确配置):<filter>
    <filter-name>struts2</filter-name>
<!--     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  --> <!-- 2.0的配置会导致 loginAction1中request为null  -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 2.1的配置正常运行 --> 
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值