我在Struts2中使用liferay,我在一个动作类中有两个动作方法,我从第一个动作方法(execute())中的一个类创建一个对象并将其传递给视图并成功显示它,在view.jsp中我'我使用该对象,但是当我提交表单并转到第二个操作方法(sendMessage())时发生了异常。
我应该怎么办?问题是什么?
struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<package namespace="/support" extends="struts-portlet-default,json-default"
name="subjectview">
<action name="index" class="com.xxx.actions.SupportFormAction"
method="execute">
<result>/html/support/view.jsp</result>
</action>
<action name="sendmsg" class="com.xxx.actions.SupportFormAction"
method="sendMessage">
<result name="success">/html/support/send-message-success-ajax.jsp</result>
<result name="error">/html/support/send-message-fail-ajax.jsp</result>
</action>
</package>
</struts>
SupportFormAction.java
package com.xxx.actions;
import com.iknito.model.SendEmail;
import com.opensymphony.xwork2.ActionSupport;
public class SupportFormAction extends ActionSupport {
private SendEmail sendEmail;
@Override
public String execute() throws Exception {
sendEmail = new SendEmail();
return "success";
}
public String sendMessage(){
try{
System.out.println(sendEmail.getName()); /* nullpointer exception happened here*/
return "success";
}
catch(Exception ex){
ex.printStackTrace();
return "error";
}
}
public SendEmail getSendEmail() {
return sendEmail;
}
public void setSendEmail(SendEmail sendEmail) {
this.sendEmail = sendEmail;
}
}
发送电子邮件.java
package iknito.com.actions;
public class SendEmail {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
视图.jsp
<%@ include file="/html/init.jsp" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form id="form1" action="addsubjects" theme="simple">
<label for="name">Name:</label>
<s:textfield type="text" name="sendEmail.name" placeholder="Hamed Yousefi" required="required"/>
<s:submit value="enter name"/>
</s:form>