私は Struts2 で liferay を使用しています。1 つのアクション クラスに 2 つのアクション メソッドがあり、最初のアクション メソッド (execute()) でクラスからオブジェクトを作成し、それをビューに渡し、view.jsp で正常に表示します。 m そのオブジェクトを使用していますが、フォームを送信して 2 番目のアクション メソッド (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;
}
}
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;
}
}
view.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>