在学习struts2拦截器token的时候,一直无法显示token的jsp页面。代码如下:
struts2.xml文件:
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.hf.action.UserAction" method="login">
<!-- 表单重复提交时,会返回invalid.token结果集 -->
<result name="invalid.token">/invalid.jsp</result>
<result name="success">/success.jsp</result>
<interceptor-ref name="token"/>
<interceptor-ref name="defaultStack"/>
</action>
</package>
login.jsp:
<form action="login.action" method="post">
<!-- 拦截器之表单重复提交 -->
<s:token></s:token>
username:<input type="text" name="user.username"/><br>
password:<input type="password" name="user.password"/><br>
<input type="submit" value="login">
</form>
LoginAction.java
public class UserAction{
private User user;
public String toLogin() {
return Action.SUCCESS;
}
public String login() {
System.out.println("user to database");
System.out.println(user);
return Action.SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
但是运行是第一次能运行成功,当表单重复提交后,出现报错:java.lang.NullPointerException
The server encountered an internal error that prevented it from fulfilling this request.
在网上第一时间也没有找到合适的解决方法,刚刚才找到答案:Action类必须继承ActionSupport类,token拦截器才会转向错误信息提示界面