如果我将isEmpty()调用到我的JSP和相应的Action类中引用的属性,我会得到一个NullPointerException。
JSP:
<%@ page contentType="text/html; charset=UTF-8"%>
<!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>Registrieren - WebShop 2.0</title>
<s:head />
</head>
<body>
<b><s:text name="welcome.title" /></b>
<s:form action="UserRegister" focusElement="firstname">
<s:textfield name="firstname" key="prompt.firstname" size="20"
required="true" />
<s:textfield name="lastname" key="prompt.lastname" size="20"
required="true" />
<s:textfield name="username" key="prompt.username" size="20"
required="true" />
<s:password name="password" key="prompt.password" size="20"
required="true" />
<br>
<s:submit method="execute" value="Registrieren" align="right" />
</s:form>
<font color="red"> <s:actionerror label="label" />
</font>
动作类:
package vislabWebShop.controller;
import vislabWebShop.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport
{
private static final long serialVersionUID = -983183915002226000L;
private String firstname;
private String lastname;
private String username;
private String password;
@Override
public String execute() throws Exception
{
if(getFirstname().isEmpty() || getLastname().isEmpty() || getUsername().isEmpty() || getPassword().isEmpty())
{
addActionError(getText("error.register.complete"));
return INPUT;
}
else
{
User registeredUser = new User();
registeredUser.setFirstname(getFirstname());
registeredUser.setLastname(getLastname());
registeredUser.setUsername(getUsername());
registeredUser.setPassword(getPassword());
addActionMessage(getText("success.register.user"));
return SUCCESS;
}
}
public String getLastname()
{
return lastname;
}
private void setLastname(String lastname)
{
this.lastname = lastname;
}
public String getFirstname()
{
return firstname;
}
private void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getUsername()
{
return (this.username);
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return (this.password);
}
public void setPassword(String password)
{
this.password = password;
}
}
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="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<!-- Add packages here -->
<package name="vislabWebShop" extends="struts-default">
<!-- Add your actions here -->
<action name="UserForward">
<result>/pages/Login.jsp</result>
</action>
<action name="UserLogin" class="vislabWebShop.controller.LoginAction">
<result name="success">/pages/Welcome.jsp</result>
<result name="input">/pages/Login.jsp</result>
</action>
<action name="UserRegister" class="vislabWebShop.controller.RegisterAction">
<result name="success">/pages/Login.jsp</result>
<result name="input">/pages/Register.jsp</result>
</action>
<action name="UserRegisterNew">
<result>/pages/Register.jsp</result>
</action>
</package>
</struts>
两个属性映射成功。属性是否也可以以其他形式复制?
答案 0 :(得分:0)
以下是错误:在RegisterAction.java
我将setter
方法放入private
,因此无法从struts
调用这些方法。将它们变成public
解决了这个问题。