使用MVC重定向到另一个SubController

时间:2016-02-12 11:21:35

标签: java url redirect model-view-controller web

我是用Java开发Web应用程序的新手,我真的在努力解决问题。我的Web应用程序需要完成两个主要任务(简而言之):

  • 用户选择位置(主要楼层,楼层1等) - >所选楼层的平面图显示 - >用户可以在计划中标记他的确切位置
  • 用户搜索其他用户 - >搜索到的用户标记的适当平面图显示

到目前为止,我的项目包含(最重要的部分):

  • FrontController

    
    
    public class FrontController extends HttpServlet {
    
      private static final String SEITE = "/WEB-INF/jsp/navi/index.jsp";
      private Map < String, Controller > controller;
    
      @
      Override
      public void init() throws ServletException {
        controller = new HashMap < String, Controller > ();
        controller.put("/start", new StartSubController(this));
        // controller.put("/floors/mainfloor", new FloorSubController(this));
        // controller.put("/floors/1og", new FloorSubController(this));
        controller.put("/floors/2og", new FloorSubController(this));
        // controller.put("/floors/3og", new FloorSubController(this));
      }
    
      @
      Override
      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    
        // /whereami/test/eins.do ohne /whereami und .do ==> /test/eins
        String navi = request.getRequestURI().substring( // /whereami/test/eins.do
          request.getContextPath().length(), // /whereami
          request.getRequestURI().length() - 3); // .do
    
        Controller c = controller.get(navi);
        if (c != null) {
          try {
            navi = c.execute(request, response);
          } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException(e);
          }
        }
    
        // Die URL f�r das Fragment
        request.setAttribute("url", "/WEB-INF/jsp" + navi + ".jsp");
    
        RequestDispatcher rd = request.getRequestDispatcher(SEITE);
        rd.forward(request, response);
      }
    
      @
      Override
      public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
        doGet(request, response);
      }
    }
    &#13;
    &#13;
    &#13;

  • 首页,用户可以在这两个目标之间进行选择(start.jsp和StartSubController) start.jsp

    &#13;
    &#13;
    <form name="sendeEtageOrUser" method="post">
      <div id="waehle-etage" style="display:none;">
        <br>Bitte geben Sie die Etage ihres aktuellen Aufenthaltsortes an:
        <select class="select-etage" name="floor">
          <option value="Erdgeschoss">Erdgeschoss</option>
          <option value="1OG">1.OG</option>
          <option value="2OG">2.OG</option>
          <option value="3OG">3.OG</option>
        </select>
        <br>
        <input id="submit-button-etage" name="geschoss-button" type="submit" value="Bestätigen"></input>
      </div>
    
      <div id="suche-user-standort" style="display:none;">
        <br>Bitte geben Sie Name oder Kennung des gesuchten Users ein:
        <input type="text" name="userNameOrKennung"></input>
        <br>
        <input id="submit-button-suche-user" name="user-button" type="submit" value="Suchen"></input>
      </div>
    </form>
    &#13;
    &#13;
    &#13;

StartSubController

&#13;
&#13;
public class StartSubController implements Controller {

  private FrontController frontController;
  private DBManager dbManager;
  private String userOrKennung;

  private final String GESCHOSS_BUTTON = "geschoss-button";
  private final String USER_BUTTON = "user-button";

  public StartSubController(FrontController frontController) {
    this.frontController = frontController;
  }

  @
  Override
  public String execute(HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    // /whereami/test/one.do without /whereami and .do ==> /test/one
    String navi = request.getRequestURI().substring( // /whereami/test/one.do
      request.getContextPath().length(), // /whereami
      request.getRequestURI().length() - 3); // .do

    if (request.getMethod().equals("POST")) {
      if (request.getParameter(GESCHOSS_BUTTON).equals("Bestätigen")) {
        // Redirection to controller and to chosen floor
        String floor = request.getParameter("floor");
        if (floor != null) {
          // TODO: Redirect to /whereiam/floors/xog.do
        }
      } else if (request.getParameter(USER_BUTTON).equals("Suchen")) {
        userOrKennung = request.getParameter("userNameOrKennung");
        try {
          if (searchUserInDB(userOrKennung) != null || searchUserInDB(userOrKennung).equals(null)) {
            // TODO: Redirect to /whereiam/floors/xog.do
          }
        } catch (SqlException e) {
          // TODO Handle SqlException
          throw (RuntimeException) new RuntimeException()
            .initCause(e);
        }
      }
    }
    return navi;
  }
}
&#13;
&#13;
&#13;

现在我的问题:我不明白如何通过StartSubController重定向到http://localhost:8080/whereAmI/floors/2og.do? 我知道我不能使用response.sendRedirect(..)和return&#34; / floors / 2og&#34;在StartSubController的execute方法中,不会更改客户端的URL。但我认为我需要更改URL以使以下FloorSubController与以下jsp一起使用。

提前致谢!

0 个答案:

没有答案