package com.zxcs.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.zxcs.interceptor.IsLoginedInterceptor;
import com.opensymphony.xwork2.ActionSupport;
import com.zxcs.bean.*;
import com.zxcs.service.UserService;
public class LoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private UserService service;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
public void validate() {
if(this.getUser().getUsername()==null||" ".equals(this.getUser().getUsername())){
this.addFieldError(this.getUser().getUsername(), "用户名不能为空!");
}
if(this.getUser().getUserpassword()==null||" ".equals(this.getUser().getUserpassword())){
this.addFieldError(this.getUser().getUsername(),"不能为空!");
}
}
@Override
public String execute() throws Exception
{
User user1 = null;
HttpServletRequest req=ServletActionContext.getRequest();
user1 = this.service.findLogin(this.getUser().getUsername(),this.getUser().getUserpassword());
if(user1!=null){
req.getSession(true).setAttribute(IsLoginedInterceptor.USER_SESSION_KEY, user1);//把用户信息保存到Session里,方便获取
return SUCCESS;
}else{
System.out.println("用户名和密码错误!");
this.addFieldError(this.getUser().getUsername(), "户名和密码错误!");
//ActionError("用户名和密码错误!");
return INPUT;
}
}
}
java.lang.NullPointerException
com.zxcs.action.LoginAction.validate(LoginAction.java:38)
com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocation(ValidationInterceptor.java:249)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:261)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
this.addFieldError(this.getUser().getUsername(), "用户名不能为空!");
//你前面都判断出this.getUser().getUsername()==null,下面还执行this.getUser().getUsername()
//能不报空么
有可能是getuser()这个为空,那怎么改呢
if(user1!=null){
第38行错误,你检查下,是不是没有获取到request对象。