花了好几个小时后,我在这里找到我的代码的解决方案。
我在Struts 2中创建了一个自定义拦截器MyInterceptor.java
,它从index.jsp页面获取参数值username和password,并将这些参数中的String转换为大写。
下面是我的拦截器代码,其中我在线号上获得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
页面,其中包含用户名和密码"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>
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's
方法findString()
未返回参数"name" and "password"
的值。
答案 0 :(得分:2)
如果你需要拦截器中的参数,你应该从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();
}
答案 1 :(得分:1)
我认为问题应该是因为拦截器的顺序。你的拦截器是最后一个拦截器,所以当请求一个动作时,
new instance
您的行动parameter interceptor
(位于default stack
中)填充操作属性。