为什么我的自定义拦截器不起作用?
Why my custom interceptor is not working?
我在 Struts 2 中创建了一个自定义拦截器 MyInterceptor.java
,它从 index.jsp
页面获取参数值 username
和 password
并将String
在这些参数中大写。
下面是我的拦截器代码,其中我在第 1 行获得 java.lang.NullPointerException
。 14
MyInterceptor.java:
package com.interceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.ActionInvocation;
public class MyInterceptor implements Interceptor{
public void init(){}
@Override
public String intercept(ActionInvocation ai) throws Exception {
ValueStack stack = ai.getStack();
String s1 = stack.findString("name");
String s2 = stack.findString("password");
String s3 = s1.toUpperCase();
String s4 = s2.toUpperCase();
stack.set("name", s3);
stack.set("password", s4);
return ai.invoke();
}
@Override
public void destroy() {
}
}
这是我的 index.jsp
页面:
需要 username
和 password
"ravi"
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<h1>Login Here</h1>
<s:form action="login">
<s:textfield name="name" label="Name"></s:textfield>
<s:textfield type="password" name="password" label="Password"></s:textfield>
<s:submit label="Login"></s:submit>
</s:form>
</body>
</html>
这里是动作classMyAction.java
:
package com.ravi.action;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport{
public String name;
public String password;
public void setName(String name){
this.name= name;
}
public void setPassword(String password){
this.password= password;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public String execute()throws Exception{
if(name.equals("RAVI") && password.equals("RAVI")){
return "success";
}
else {
return "error";
}
}
}
这是我的 struts.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors >
<interceptor name="uppername" class="com.interceptor.MyInterceptor"></interceptor>
</interceptors>
<action name="login" class="com.ravi.action.MyAction">
<interceptor-ref name="uppername"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success" >welcome.jsp</result>
<result name="error">Error.jsp</result>
</action>
</package>
</struts>
Stacktrace如下:
java.lang.NullPointerException
com.interceptor.MyInterceptor.intercept(MyInterceptor.java:14)
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:567)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:1)
java.lang.Thread.run(Unknown Source)
我猜问题出在访问 MyInterceptor.java
中的请求参数上。 ValueStack
的方法 findString()
没有返回参数 name
和 password
的值。
如果您需要拦截器中的参数,您应该从 action context 中获取它。
Map<String, Object> params = ActionContext.getContext().getParameters();
您在浏览 index.jsp
时试图从值堆栈中获取属性(而不是参数),但值堆栈没有此类键。
String[] s1 = (String[]) params.get("name");
String[] s2 = (String[]) params.get("password");
if (s1 != null && s2 != null) {
s1[0] = s1[0].toUpperCase();
s2[0] = s2[0].toUpperCase();
}
我认为应该是拦截器顺序的问题。你的拦截器是最后一个拦截器,所以当一个动作被请求时,
- Struts
new instance
你的行动
parameter interceptor
(在 default stack
中)填充操作属性。
- 之后您的拦截器将更新值堆栈,当然,没有人会使用它。
我在 Struts 2 中创建了一个自定义拦截器 MyInterceptor.java
,它从 index.jsp
页面获取参数值 username
和 password
并将String
在这些参数中大写。
下面是我的拦截器代码,其中我在第 1 行获得 java.lang.NullPointerException
。 14
MyInterceptor.java:
package com.interceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.ActionInvocation;
public class MyInterceptor implements Interceptor{
public void init(){}
@Override
public String intercept(ActionInvocation ai) throws Exception {
ValueStack stack = ai.getStack();
String s1 = stack.findString("name");
String s2 = stack.findString("password");
String s3 = s1.toUpperCase();
String s4 = s2.toUpperCase();
stack.set("name", s3);
stack.set("password", s4);
return ai.invoke();
}
@Override
public void destroy() {
}
}
这是我的 index.jsp
页面:
需要 username
和 password
"ravi"
:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<h1>Login Here</h1>
<s:form action="login">
<s:textfield name="name" label="Name"></s:textfield>
<s:textfield type="password" name="password" label="Password"></s:textfield>
<s:submit label="Login"></s:submit>
</s:form>
</body>
</html>
这里是动作classMyAction.java
:
package com.ravi.action;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport{
public String name;
public String password;
public void setName(String name){
this.name= name;
}
public void setPassword(String password){
this.password= password;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public String execute()throws Exception{
if(name.equals("RAVI") && password.equals("RAVI")){
return "success";
}
else {
return "error";
}
}
}
这是我的 struts.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors >
<interceptor name="uppername" class="com.interceptor.MyInterceptor"></interceptor>
</interceptors>
<action name="login" class="com.ravi.action.MyAction">
<interceptor-ref name="uppername"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success" >welcome.jsp</result>
<result name="error">Error.jsp</result>
</action>
</package>
</struts>
Stacktrace如下:
java.lang.NullPointerException
com.interceptor.MyInterceptor.intercept(MyInterceptor.java:14)
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:567)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:1)
java.lang.Thread.run(Unknown Source)
我猜问题出在访问 MyInterceptor.java
中的请求参数上。 ValueStack
的方法 findString()
没有返回参数 name
和 password
的值。
如果您需要拦截器中的参数,您应该从 action context 中获取它。
Map<String, Object> params = ActionContext.getContext().getParameters();
您在浏览 index.jsp
时试图从值堆栈中获取属性(而不是参数),但值堆栈没有此类键。
String[] s1 = (String[]) params.get("name");
String[] s2 = (String[]) params.get("password");
if (s1 != null && s2 != null) {
s1[0] = s1[0].toUpperCase();
s2[0] = s2[0].toUpperCase();
}
我认为应该是拦截器顺序的问题。你的拦截器是最后一个拦截器,所以当一个动作被请求时,
- Struts
new instance
你的行动 parameter interceptor
(在default stack
中)填充操作属性。- 之后您的拦截器将更新值堆栈,当然,没有人会使用它。