Struts2的Action使用validate()方法,提示空指针错误!
我建有Tuser.java、packagecom.struts2.pojo;publicclassTuser{privateintid;privateStringname...
我建有Tuser.java、
package com.struts2.pojo;
public class Tuser {
private int id;
private String name;
private String password;
private String ds;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setDs(String ds) {
this.ds = ds;
}
public String getDs() {
return ds;
}
}
但是:LoginAction.java
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private TuserService tuserService;
private Tuser tuser;
public TuserService getTuserService() {
return tuserService;
}
public void setTuserService(TuserService tuserService) {
this.tuserService = tuserService;
}
public Tuser getTuser() {
return tuser;
}
public void setTuser(Tuser tuser) {
this.tuser = tuser;
}
public String execute() throws Exception {
if("hello".equals(this.tuser.getName().trim())&&"word".equals(this.tuser.getPassword().trim())){
return SUCCESS;
}
else{
this.addFieldError("name", "用户名或者密码错误!");
return ERROR;
}
}
public void validate(){
if(this.tuser.getName() == null||"".equals(this.tuser.getName().trim())){
this.addFieldError("name", "请输入用户名");
}
if(this.tuser.getPassword() == null ||"".equals(this.tuser.getPassword().trim())){
this.addFieldError("password", "请输入密码");
}
}
}
运行时,MyEclipse不报错误,只是网页显示如下错误:
exception
java.lang.NullPointerException
com.struts.action.LoginAction.validate(LoginAction.java:46)
com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocat
。。。。。。。。。。。。。。。。。。。。。。。。。
取不到值,范例是在Action写Tuser的getter和setter方法,我是import进来的!有问题吗? 展开
package com.struts2.pojo;
public class Tuser {
private int id;
private String name;
private String password;
private String ds;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setDs(String ds) {
this.ds = ds;
}
public String getDs() {
return ds;
}
}
但是:LoginAction.java
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private TuserService tuserService;
private Tuser tuser;
public TuserService getTuserService() {
return tuserService;
}
public void setTuserService(TuserService tuserService) {
this.tuserService = tuserService;
}
public Tuser getTuser() {
return tuser;
}
public void setTuser(Tuser tuser) {
this.tuser = tuser;
}
public String execute() throws Exception {
if("hello".equals(this.tuser.getName().trim())&&"word".equals(this.tuser.getPassword().trim())){
return SUCCESS;
}
else{
this.addFieldError("name", "用户名或者密码错误!");
return ERROR;
}
}
public void validate(){
if(this.tuser.getName() == null||"".equals(this.tuser.getName().trim())){
this.addFieldError("name", "请输入用户名");
}
if(this.tuser.getPassword() == null ||"".equals(this.tuser.getPassword().trim())){
this.addFieldError("password", "请输入密码");
}
}
}
运行时,MyEclipse不报错误,只是网页显示如下错误:
exception
java.lang.NullPointerException
com.struts.action.LoginAction.validate(LoginAction.java:46)
com.opensymphony.xwork2.validator.ValidationInterceptor.doBeforeInvocat
。。。。。。。。。。。。。。。。。。。。。。。。。
取不到值,范例是在Action写Tuser的getter和setter方法,我是import进来的!有问题吗? 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询