0

我在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>
4

1 回答 1

0

您的对象在 JSP 页面上呈现后丢失。

你需要要么

  • 将其放入页面中,例如隐藏输入中,然后完全重新传输(在您的情况下,对象的每个属性都有一个隐藏参数)
  • 或者(当有一个完整的对象而不是一个简单的字符串时会更好)把它放在会话中,然后再检索它。
于 2015-08-15T14:59:19.803 回答