为什么运行时方法调用会出错?<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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><s:text name="LoginPage"/></title>
</head>
<body>
<form action="LoginRegist!login.action" method="post">
<tr>
<td><s:text name="UserName"/></td>
<td><input type="text" name="LoginuserName"/></td>
</tr>
<tr>
<td><s:text name="UserPass"/></td>
<td><input type="text" name="LoginuserPassword"/></td>
</tr>
<tr>
<input type="submit" value="<s:text name="Submit"/>" onClick="login()"/>
</tr>
</form>
</body>
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.enable.DynamicMethodInvocation" value="true"/> <package name="LR" extends="struts-default">
<action name="LoginRegist" class="com.struts2.LoginRegistAction" method="login">
<result name="input">/Login.jsp</result>
<result>/WEB-INF/welcome.jsp</result>
<result name="error">/Login.jsp</result>
</action>
<action name="LoginRegistRegist" class="com.struts2.LoginRegistAction" method="regist">
<result name="input">/regist.jsp</result>
<result>/WEB-INF/welcome.jsp</result>
</action>
</package>
<package name="GB" extends="struts-default">
<action name="GetBooks" class="com.struts2.GetBooksAction">
<result name="input">/Login.jsp</result>
<result>/WEB-INF/booksList.jsp</result>
<result name="login">/Login.jsp</result>
</action>
</package>
</struts>LoginRegistAction.java:
package com.struts2;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.interceptor.ServletResponseAware;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;public class LoginRegistAction extends ActionSupport implements ModelDriven<LoginUser>, ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = 1L;
int counter;
LoginUser loginUs = new LoginUser();
@Override
public LoginUser getModel() {
if(loginUs == null) loginUs = new LoginUser();
return loginUs;
}
private String RegistuserName;
private String RegistuserPassword;
public void setRegistUserName(String RegistuserName) { this.RegistuserName = RegistuserName; }
public String getRegistUserName() { return this.RegistuserName; }
public void setRegistUserPassword(String RegistuserPassword) { this.RegistuserPassword = RegistuserPassword; }
public String getRegistUserPassword() { return this.RegistuserPassword; }
private HttpServletResponse response;
public void validation() {
if(loginUs.getLoginUserName() == null || loginUs.getLoginUserName().equals(""))
addFieldError("LoginuserName", getText("user.required"));
if(loginUs.getLoginUserPassword() == null || loginUs.getLoginUserPassword().equals(""))
addFieldError("LoginuserPassword", getText("pass.required"));
if(RegistuserName == null || RegistuserName.equals(""))
addFieldError("RegistuserName", getText("user.required"));
if(RegistuserPassword == null || RegistuserPassword.equals(""))
addFieldError("RegistuserPassword", getText("pass.required"));
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String login() throws Exception {
if(loginUs.getLoginUserName().equals("QQAA2233") && loginUs.getLoginUserPassword().equals("3322QQAA")) {
ActionContext ac = ActionContext.getContext();
// 递增访问用户次数
try {
counter = Integer.parseInt(ac.getApplication().get("counter").toString());
} catch(Exception e) {
e.printStackTrace();
}
++counter;
ac.getApplication().put("counter", new Integer(counter));
// 用cookies载入显示userName
/*Cookie c = new Cookie("user", loginUs.getUserName());
c.setMaxAge(60*60);
response.addCookie(c);*/
ac.getSession().put("LoginuserName", loginUs.getLoginUserName());
return SUCCESS;
}
else
return ERROR;
}
public String regist() throws Exception {
if(getRegistUserName() != null && !getRegistUserName().equals("") &&
getRegistUserPassword() != null && !getRegistUserPassword().equals("")) {
ActionContext ac = ActionContext.getContext();
ac.getSession().put("LoginuserName", getRegistUserName());
return SUCCESS;
}
else
return INPUT;
}
}regist.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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><s:text name="RegistPage"/></title>
</head>
<body>
<form action="LoginRegistRegist!regist.action" method="post">
<table border="0">
<tr>
<td><s:text name="UserName"/></td>
<td><input type="text" name="RegistuserName"/></td>
</tr>
<tr>
<td><s:text name="UserPass"/></td>
<td><input type="text" name="RegistuserPassword"/></td>
</tr>
<tr>
<input type="submit" name="<s:text name="Regist"/>"/>
<input type="reset" name="<s:text name="Reset"/>"/>
</tr>
</table>
</form>
</body>
</html>登录界面转向login.action时运行错误为:HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
com.struts2.LoginRegistAction.login(LoginRegistAction.java:66)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
连注册界面转向regist.action时也出错好似识别不了这种方法调用那样为什么
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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><s:text name="LoginPage"/></title>
</head>
<body>
<form action="LoginRegist!login.action" method="post">
<tr>
<td><s:text name="UserName"/></td>
<td><input type="text" name="LoginuserName"/></td>
</tr>
<tr>
<td><s:text name="UserPass"/></td>
<td><input type="text" name="LoginuserPassword"/></td>
</tr>
<tr>
<input type="submit" value="<s:text name="Submit"/>" onClick="login()"/>
</tr>
</form>
</body>
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.enable.DynamicMethodInvocation" value="true"/> <package name="LR" extends="struts-default">
<action name="LoginRegist" class="com.struts2.LoginRegistAction" method="login">
<result name="input">/Login.jsp</result>
<result>/WEB-INF/welcome.jsp</result>
<result name="error">/Login.jsp</result>
</action>
<action name="LoginRegistRegist" class="com.struts2.LoginRegistAction" method="regist">
<result name="input">/regist.jsp</result>
<result>/WEB-INF/welcome.jsp</result>
</action>
</package>
<package name="GB" extends="struts-default">
<action name="GetBooks" class="com.struts2.GetBooksAction">
<result name="input">/Login.jsp</result>
<result>/WEB-INF/booksList.jsp</result>
<result name="login">/Login.jsp</result>
</action>
</package>
</struts>LoginRegistAction.java:
package com.struts2;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;import org.apache.struts2.interceptor.ServletResponseAware;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;public class LoginRegistAction extends ActionSupport implements ModelDriven<LoginUser>, ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = 1L;
int counter;
LoginUser loginUs = new LoginUser();
@Override
public LoginUser getModel() {
if(loginUs == null) loginUs = new LoginUser();
return loginUs;
}
private String RegistuserName;
private String RegistuserPassword;
public void setRegistUserName(String RegistuserName) { this.RegistuserName = RegistuserName; }
public String getRegistUserName() { return this.RegistuserName; }
public void setRegistUserPassword(String RegistuserPassword) { this.RegistuserPassword = RegistuserPassword; }
public String getRegistUserPassword() { return this.RegistuserPassword; }
private HttpServletResponse response;
public void validation() {
if(loginUs.getLoginUserName() == null || loginUs.getLoginUserName().equals(""))
addFieldError("LoginuserName", getText("user.required"));
if(loginUs.getLoginUserPassword() == null || loginUs.getLoginUserPassword().equals(""))
addFieldError("LoginuserPassword", getText("pass.required"));
if(RegistuserName == null || RegistuserName.equals(""))
addFieldError("RegistuserName", getText("user.required"));
if(RegistuserPassword == null || RegistuserPassword.equals(""))
addFieldError("RegistuserPassword", getText("pass.required"));
}
@Override
public void setServletResponse(HttpServletResponse response) {
this.response = response;
}
public String login() throws Exception {
if(loginUs.getLoginUserName().equals("QQAA2233") && loginUs.getLoginUserPassword().equals("3322QQAA")) {
ActionContext ac = ActionContext.getContext();
// 递增访问用户次数
try {
counter = Integer.parseInt(ac.getApplication().get("counter").toString());
} catch(Exception e) {
e.printStackTrace();
}
++counter;
ac.getApplication().put("counter", new Integer(counter));
// 用cookies载入显示userName
/*Cookie c = new Cookie("user", loginUs.getUserName());
c.setMaxAge(60*60);
response.addCookie(c);*/
ac.getSession().put("LoginuserName", loginUs.getLoginUserName());
return SUCCESS;
}
else
return ERROR;
}
public String regist() throws Exception {
if(getRegistUserName() != null && !getRegistUserName().equals("") &&
getRegistUserPassword() != null && !getRegistUserPassword().equals("")) {
ActionContext ac = ActionContext.getContext();
ac.getSession().put("LoginuserName", getRegistUserName());
return SUCCESS;
}
else
return INPUT;
}
}regist.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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><s:text name="RegistPage"/></title>
</head>
<body>
<form action="LoginRegistRegist!regist.action" method="post">
<table border="0">
<tr>
<td><s:text name="UserName"/></td>
<td><input type="text" name="RegistuserName"/></td>
</tr>
<tr>
<td><s:text name="UserPass"/></td>
<td><input type="text" name="RegistuserPassword"/></td>
</tr>
<tr>
<input type="submit" name="<s:text name="Regist"/>"/>
<input type="reset" name="<s:text name="Reset"/>"/>
</tr>
</table>
</form>
</body>
</html>登录界面转向login.action时运行错误为:HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
com.struts2.LoginRegistAction.login(LoginRegistAction.java:66)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
连注册界面转向regist.action时也出错好似识别不了这种方法调用那样为什么
if("QQAA2233".equals(loginUs.getLoginUserName()) && "3322QQAA".equals(loginUs.getLoginUserPassword()) {
com.struts2.LoginRegistAction.login(LoginRegistAction.java:66)
应该比较的参数不对,在66行打个断点调试。
这个loginUs的setter从来没有被调用过吧。
看看有没有有效。默认应该是有效。2) LoginUser 类怎么定义的?2) 其他还没怎么看出来。LoginUserName的命名比较不地道。地道点应该是变量和jsp里为 loginUserName,
Action里 getLoginUserName, setLoginUserName。注意大小写。这些都不解决再说。
这个我本来的意思为无论登录还是注册最终的用户名都归为loginUserName中,因为成功后都转向welcome.jsp页面中显示