簡體   English   中英

Liferay:如何從JSP頁面調用Servlet

[英]Liferay : How to call a Servlet from a JSP Page

這是我的第一個Portlet。 我沒有在servlet中獲取值。 請看節目。 在我的自定義portlet Java類doView()方法中,我展示了一個JSP頁面

public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {            
    include(viewJSP, renderRequest, renderResponse);
}

view.jsp頁面中,我引用一個servlet來接收值:

<form action="formServlet" method="post">
    <h1>Please Login</h1>
    Login:    <input type="text" name="login"><br>
    Password: <input type="password" name="password"><br>
    <input type=submit value="Login">
</form>

web.xml文件中:

<servlet>
    <servlet-name>formServlet</servlet-name>
    <servlet-class>FormServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>formServlet</servlet-name>
    <url-pattern>formServlet</url-pattern>
</servlet-mapping>

在我的servlet里面

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {  
    String name = (String)request.getParameter("login");
    System.out.println("The Name is "+name);    
}

但我不知道為什么沒有調用servlet。

注意這是一個有點復雜問題的答案。 如果您正在嘗試學習portlet創建的基礎知識,我在另一個問題中發布了一個更好的答案


您正在使用POST方法提交表單,但您的servlet只是實現了為GET方法提供服務的doGet() 您應該使用GET提交表單或實現doPost()方法(在其他情況下這將是更好的選擇)。

此外,如果它是絕對模式,則必須在<url-pattern>內容之前加斜杠。 也就是說,它應該是

<url-pattern>/formServlet</url-pattern>

代替

<url-pattern>formServlet</url-pattern>

那就是說, 現在忘了servlets了!

你是以最糟糕的方式之一做到的。 編寫一個調用servlet的portlet真是個壞主意。 經過很長一段時間與Liferay合作,我可以想象它或多或少會合理的情況,但它不在這里,也不會在大多數時候。

那么, 你應該怎么做? 您應該將表單提交到操作URL 要做到這一點,首先在JSP中包含portlet taglib:

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

現在, <portlet:actionURL /> 替換表單的action 此標記將替換為門戶網站生成的特殊URL。 此外, 每個輸入名稱前面加上標記<portlet:namespace /> ; 你的<input type="text" name="login">應該變成<input type="text" name="<portlet:namespace />login"> 此標記將替換為僅與您的portlet關聯的字符串; 因為頁面中可以有很多portlet,所以每個輸入都應該指定它來自哪個portlet。 這是最終結果:

<form action="<portlet:actionURL />" method="post">
    <h1>Please Login</h1>
    Login:    <input type="text" name="<portlet:namespace />login"><br>
    Password: <input type="password" name="<portlet:namespace />password"><br>
    <input type=submit value="Login">
</form>

現在您要正確提交數據 - 但如何獲取提交的數據? 當然沒有必要使用servlet! 而是向您的自定義portlet類添加一個名為processAction()的方法。 此方法應返回void並接收兩個參數,即javax.portlet.ActionRequestjavax.portlet.ActionResponse 這是一個空的processAction()示例:

public void processAction(ActionRequest request, ActionResponse response) {
    // Nothing to be done for now.
}

當對操作URL的請求(由<portlet:actionURL />生成的請求)發送到服務器時,它首先由processAction()方法處理,然后由doView() 因此,您在servlet中編寫的代碼應該放在 processAction() 結果應該是:

public void processAction(ActionRequest request, ActionResponse response) {
    String name = (String)request.getParameter("login");
    System.out.println("The Name is "+name);
}

嘗試一下,你會發現它會很好用。

yyy - 以下是您的評論的答案:

在JSP頁面中,您需要為希望該portlet執行的每個操作添加以下內容:

<portlet:actionURL var="addUserURL">
<portlet:param name="<%= ActionRequest.ACTION_NAME %>" value="addUser" />
</portlet:actionURL>

<form method="post" action="<%= addUserURL %>">

然后在你的com.test.Greeting portlet中,每個都有:

public void addUser (ActionRequest actionRequest, ActionResponse actionResponse) {}

這回答了你的問題了嗎?

此外,通常最好開始一個新問題,而不是添加評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM