我在 Struts 2 中创建了一个自定义拦截器
MyInterceptor.java
,它从 username
页面获取参数值 password
和 index.jsp
,并将这些参数中的 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>
这是动作类
MyAction.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>
堆栈跟踪如下:
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();
}
我认为问题应该是因为拦截器的顺序。您的拦截器是最后一个拦截器,因此当请求操作时,
new instance
parameter interceptor
(位于 default stack
中)填充操作属性。