找到你要的答案

Q:two action method in an action class using the same stack in liferay struts2 [duplicate]

Q:在Action类中使用相同的堆栈在Liferay [复制]两Struts2动作方法

i am using liferay with Struts2, i have two action method in one action class, i create an object from a class in first action method(execute()) and passing that to the view and showing it successfully, in view.jsp i'm using that object but when i submit the form and going to second action method(sendMessage()) an exception happened.

what should i do? what is the problem?

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>

我使用Liferay与struts2,我有一个动作类的两个动作方法,我创建了一个对象从一个类中第一个动作方法(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;
    }
}

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>
answer1: 回答1:

Your object is lost after it is rendered on JSP page.

You need to either

  • put it in the page, for example in hidden inputs, and re-trasmit it completely (in your case, one hidden parameter for each attribute of the object)
  • or (much better when there is an entire object and not a simple String) put it in the session, and retrieve it later.

你的目标是经过对JSP页面渲染的失去。

你也需要

  • put it in the page, for example in hidden inputs, and re-trasmit it completely (in your case, one hidden parameter for each attribute of the object)
  • or (much better when there is an entire object and not a simple String) put it in the session, and retrieve it later.
java  jsp  struts2  liferay  struts2-interceptors