使用response.sendRedirect

时间:2015-11-06 20:57:31

标签: java redirect mobile liferay portlet

我正在尝试将移动网络应用程序移植到liferay中。我设法移植了大部分代码,但问题是转换仍然全屏。

我已经阅读了有关使用portlet的各种帖子:actionURL,这似乎解决了一个更长的用户,其中包含其他参数,但它会将我的浏览器抛入一个空白的循环中。

的login.jsp

<form id="loginForm" action="<%= PortalUtil.getPathContext(request) + "/Submit" %>" method="post" autocomplete="off">
    <label for="userid">User Number</label>
    <input type="number" name="userid" id="userid" pattern="[0-9]*" value="">

    <label for="userpass">Password</label>
    <input type="password" name="userpass" id="userpass" value="">

    <button type="submit" name="submit" value="submit" data-theme="a">Submit</button>
</form>

SubmitForm.java

package controller;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

@WebServlet("/Submit")
public class SubmitForm extends HttpServlet {
    public HttpSession getSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Do required initialization
        HttpSession session = null;
        session = request.getSession(false);
        return session;
    }

    // POST Method, Servlet contains NO GET Method
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = "";
        try {
            HttpSession session = getSession(request, response);
            String user = session.getAttribute("user");
            session.setAttribute("user", user);
            session.setAttribute("redirect", "Home");
            //session.setAttribute("redirect", "Status");
            //session.setAttribute("redirect", "Login");
            //session.setAttribute("redirect", "Error");
            response.sendRedirect("redirect.jsp");
        } catch (ServletException e) {
            System.err.println("ServletException:", e);
        } catch (IOException e) {
            System.err.println("IOException:", e);
        }
        return;
    }
}

redirect.jsp中

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String url = (String) session.getAttribute("redirect");
    System.out.println("redirect.jsp:url=" + url);
    if (null == url)
      url = "index.jsp";
    else
      session.removeAttribute("redirect");
%>
<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <script type="text/javascript">
      window.location.href = "<%=url%>";
    </script>
  </body>
</html>

的web.xml

<servlet>
  <servlet-name>status-jsp</servlet-name>
  <jsp-file>/status.jsp</jsp-file>
</servlet>
<servlet-mapping><servlet-name>status-jsp</servlet-name><url-pattern>/Status</url-pattern></servlet-mapping>
<servlet>
  <servlet-name>home-jsp</servlet-name>
  <jsp-file>/home.jsp</jsp-file>
</servlet>
<servlet-mapping><servlet-name>home-jsp</servlet-name><url-pattern>/Home</url-pattern></servlet-mapping>
<servlet>
  <servlet-name>login-jsp</servlet-name>
  <jsp-file>/login.jsp</jsp-file>
</servlet>
<servlet-mapping><servlet-name>login-jsp</servlet-name><url-pattern>/Login</url-pattern></servlet-mapping>
<servlet>
  <servlet-name>error-jsp</servlet-name>
  <jsp-file>/error.jsp</jsp-file>
</servlet>
<servlet-mapping><servlet-name>error-jsp</servlet-name><url-pattern>/Error</url-pattern></servlet-mapping>

新 redirect.jsp中

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String url = (String) session.getAttribute("redirect");
    System.out.println("redirect.jsp:url=" + url);
    if (null == url) {
        url = "index.jsp";
    } else {
        session.removeAttribute("redirect");
    }
%>
<portlet:actionURL var="portleturl" >
  <portlet:param name="jspPage" value="/portlet/<%=url%>"/>
</portlet:actionURL>
<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <script type="text/javascript">
            //window.location.href = "<%=url%>";
            console.log("${portleturl}");
            window.location.href = "${portleturl}";
        </script>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

我完全不确定我明白你的意思。但我认为你的意思是展示一个不同的jsp,如果那是doView方法中的问题你可以做到这一点

final String jspurl = "/html/page/page.jsp";

                getPortletContext().getRequestDispatcher(jspurl).include(renderRequest, renderResponse);

这将呈现page.jsp而不是当前渲染的portlet.xml中指定的jsp

答案 1 :(得分:0)

你似乎正在servlet中实现某种简化登录 - 正如你提到的Liferay:Liferay已经很好地处理了身份管理,而且我不明白为什么你要创建自己的身份管理它。此外,您在门户中实现了一个servlet - 这并没有多大意义。当你从servlet的JSP创建一个portletURL(例如actionURL)时,我不确定portletURL的所有必要上下文是否都可以在servlet中使用。

恕我直言,你应该

  • 将身份管理留给Liferay(或者当您想要重新实现底层基础设施时,不要使用门户网站)
  • 如果您坚持使用Liferay:在portlet中实现您的功能,并直接POST到actionURL而不是servlet。
Liferay的文档有几个分步说明和文档,例如: Learning PathsTutorials