JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [發問]struts2-欄截器Interceptor,接收前端參數發生java.lang.NullPointerException
ttnezpwinf





發文: 20
積分: 0
於 2019-03-02 13:47 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位好,最近在學習struts2,
在攔截器接收前端參數,但是我遇到了以下的錯誤訊息:


java.lang.NullPointerException
  com.inter.CheckInterceptor.intercept(CheckInterceptor.java:29)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:168)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
  org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:76)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:229)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  org.apache.struts2.interceptor.DateTextFieldInterceptor.intercept(DateTextFieldInterceptor.java:125)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:253)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:140)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:245)
  org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
  org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:575)
  org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)


請教各位前輩在攔截器如何接收前端的參數,
以下是我開發時的相關程式碼:

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

login.jsp (JSP網頁-輸入登入資訊)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>login.jsp</title>
  </head>
  <body>
    <h1>使用者登入</h1>
    <font color="red">${requestScope.tip}</font>
    <s:form id="id" action="login">
      <s:textfield name="user.name" label="使用者名稱" />
      <s:password name="user.pass" label="密碼" />
      <s:submit value="註冊" />
    </s:form>
  </body>
</html>


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

CheckInterceptor.java (攔截器-接收前端網頁傳來的參數)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.inter;
 
import java.util.Map;
 
import com.obj.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 
public class CheckInterceptor extends AbstractInterceptor{
 
  private static final long serialVersionUID = 1;
  private User user;
  
  public User getUser() {
    return user;
  }
 
  public void setUser(User user) {
    this.user = user;
  }
 
  @Override
  public String intercept(ActionInvocation arg0) throws Exception {
    ActionContext actionContext = arg0.getInvocationContext();
    
    Map<?, ?> sessionMap = actionContext.getSession();
    String name = user.getName();  //這一行會出錯 
    
    if(name != null && name.equals("ttnezpwinf")){
      return arg0.invoke();
    }
    actionContext.put("tip", "請先登入!");
    return Action.LOGIN;
  }
}


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

User.java (這是一個JavaBean)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.obj;
 
public class User {
  private String name;
  private String pass;
  
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPass() {
    return pass;
  }
  public void setPass(String pass) {
    this.pass = pass;
  }
  public User(){
    super();
  }
}


reply to postreply to post
» JWorld@TW »  Java 新手區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8