Struts2拦截器的问题...

zx44y 2013-02-08 07:30:48
一个SSH2的小项目..准备是配上拦截器的。
发现配上拦截器以后...进入Action就会报空指针错。

我以为是Spring的问题,但是是可以到达Action的,但是Service类会报空指针错。



下面是Struts2配置文件的代码:

<struts>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.action.extension" value="do"/>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="LoginInterceptor" class="LoginInterceptor" />
<interceptor-stack name="defaultInterceptor">
<interceptor-ref name="LoginInterceptor" />
</interceptor-stack>
</interceptors>
<!-- 定义默认拦截器 -->
<default-interceptor-ref name="LoginInterceptor" />

<!-- 定义全局处理结果 -->
<global-results>
<!-- 逻辑名为login的结果,映射到/login.jsp页面 -->
<result name="login">/index.jsp</result>
</global-results>

<action name="login" class="UserAction" method="LoginCheck">
<result name="success" type="redirectAction">write</result>
</action>
<action name="manage" class="ArticleAction" method="ArticleList">
<result name="success" type="freemarker">/WEB-INF/ftl/manage.ftl</result>
<action name="write" class="ArticleAction" method="ArticleWrite">
<result name="success" type="freemarker">/WEB-INF/ftl/write.ftl</result>
</action>
</action>
</struts>


拦截器代码:
@Component("LoginInterceptor")
public class LoginInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map<String, Object> session = invocation.getInvocationContext().getSession();
String LoginSign = (String) session.get("LoginSign");
if (null != LoginSign && LoginSign.equals("Logined")) {
System.out.println("拦截器:合法用户登录---");
String result = invocation.invoke();
return result;
} else {
System.out.println("拦截器:用户未登录---");
return Action.LOGIN;
}
}
}


登录的代码:
@Controller("UserAction")
@Scope("prototype")
public class UserAction extends BaseAction{
private static final long serialVersionUID = 1L;
private UserService userService;
public String LoginCheck(){
Author author = new Author();
author.setName(request.getParameter("username"));
author.setPassword(request.getParameter("password"));
if(userService.login(author)){
session.put("LoginSign", "Logined");
return SUCCESS;
}else{
return LOGIN;
}
}


登陆后的访问页面代码:

@Controller("ArticleAction")
@Scope("prototype")
public class ArticleAction extends BaseAction{
private static final long serialVersionUID = 1L;
private ArticleService articleService;
private CategoryService categoryService;
private String result;

public String ArticleWrite() throws Exception {
List<Category> list = categoryService.findAll();
request.setAttribute("clist", list);
return SUCCESS;
}

@Resource(name="articleService")
public void setArticleService(ArticleService articleService) {
this.articleService = articleService;
}

@Resource(name="categoryService")
public void setCategoryService(CategoryService categoryService) {
this.categoryService = categoryService;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}
}
...全文
217 5 点赞 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2013-02-08 07:30
社区公告
暂无公告