如何在不使用会话的情况下在liferay中显示成功消息?

时间:2015-11-13 08:49:40

标签: java html liferay portal

这是我在本网站的第一个问题。请忽略我的英语语法错误。

我在页面中提交答案后尝试在liferay中显示成功和错误消息而不使用会话。

我正在使用liferay6.2CE

我的控制器在下面。

的TestController

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.techm.test1.service.Service;
public class TestController extends MVCPortlet{
    protected void include(String path,RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException{  // include methos starts here
        PortletRequestDispatcher reqDispatcher = getPortletContext().getRequestDispatcher(path);
        if(reqDispatcher!=null) {
            reqDispatcher.include(renderRequest, renderResponse);
        }
    }
    public void processAction(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, PortletException 
    {

        String userId = actionRequest.getRemoteUser();

        Service s=new Service();
        List outputList=new ArrayList();

        String Question1 = ParamUtil.getString(actionRequest, "Question1");
        outputList.add(Question1);

        String Question2 = ParamUtil.getString(actionRequest, "Question2");
        outputList.add(Question2);

        String Question3 = ParamUtil.getString(actionRequest, "Question3");
        outputList.add(Question3);
outputList.add( ParamUtil.getString(actionRequest, "Question15"));
        //outputList.add(Question15);

        Boolean bl=s.getoutputList(outputList,userId);
}
}

view.jsp的

<%@page import="com.techm.test1.model.Model"%>
<%@page import="java.util.List"%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<portlet:defineObjects />


This is the
<b>Test !!</b>
portlet.
<div>

    <portlet:actionURL var="submitFormURL"></portlet:actionURL>
    <form method="POST" action=<%=submitFormURL%>>
        1.Predict the output of following Java program
        <pre class="brush:java">
class Main {
    public static void main(String args[]) {
        try {
            throw 10;
        }
        catch(int e) {
            System.out.println("Got the Exception " + e);
        }
    }
}

<br />
        </pre>
        <input type="radio" value="Got Exception 10"
            name="<portlet:namespace/>Question1" />Got Exception 10</br> <input
            type="radio" value="Got Exception 0"
            name="<portlet:namespace/>Question1" />Got Exception 0</br> <input
            type="radio" value="Compiler Error"
            name="<portlet:namespace/>Question1" />Compiler Error</br> </br>2. Which of
        the following is true about inheritance in Java? <br />
        <pre class="brush:java">
1) Private methods are final.
2) Protected members are accessible within a package and 
   Inherited classes outside the package.
3) Protected methods are final.
4) We cannot override private methods. 
<br />

        </pre>
        <input type="radio" value="1" name="<portlet:namespace/>Question2" />1</br>
        <input type="radio" value="Only 1 2"
            name="<portlet:namespace/>Question2" />Only 1, 2</br> <input type="radio"
            value="1,2 and 3" name="<portlet:namespace/>Question2" />1,2, 3</br></br>
        3.Output of follwoing Java program<br />
        <pre class="brush:java">
class Main {
    public static void main(String args[]) {
        final int i;
        i = 20;
        System.out.println(i);
    }
}
<br />
        </pre>
        <input type="radio" value="1" name="<portlet:namespace/>Question3" />1</br>
        <input type="radio" value="Only 1 2"
            name="<portlet:namespace/>Question3" />Only 1, 2</br> <input type="radio"
            value="1,2 and 3" name="<portlet:namespace/>Question3" />1,2, 3</br></br>

        <input type="submit" id="submit" value="Submit">

    </form>
</div>

怎么做?

任何帮助..

1 个答案:

答案 0 :(得分:0)

我明白了

processAction 方法中,我使用了以下代码。

Boolean bl=s.getoutputList(outputList,userId);

        if(!bl){
            actionResponse.setRenderParameter("Result", TestDetailsConstants.TEST_ERROR_MSG);
        }
        else{
            actionResponse.setRenderParameter("Result",TestDetailsConstants.TEST_SUCCESS_MSG);
        }

我在 doView 方法中调用它并将msg传递给jsp并显示

public void doView(RenderRequest renderRequest, RenderResponse renderResponse ) throws IOException, PortletException
    {
        String viewPage="";
        String s="";
        try
        {

        viewPage = "/view.jsp";
         s=renderRequest.getParameter("Result");

        renderRequest.setAttribute("Strimgmesg",s );
        }// try block ends here
        catch (Exception e) 
        {
            e.printStackTrace();
        }// catch block ends here

        finally {

            include(viewPage,renderRequest,renderResponse);
        }
    } 

在JSP中

</br> <input type="submit" id="submit" value="Submit">
        <%
            String a = (String) renderRequest.getAttribute("Strimgmesg");
        %>
        <%
            if (a != null) {
        %>
        <%=a%>
        <%
            }
        %>