SprinMVC控制流程

时间:2016-03-09 11:52:09

标签: java spring spring-mvc model-view-controller

我想重写Spring MVC中的servlet控制流这是我的 在Servlet中的doGet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        String path = request.getParameter("path");

        if (path != null && path.equals("register")) {
            RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/views/system/registeruser.jsp");
            view.forward(request, response);
        } else if (path != null && path.equals("usermang")) {
            RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/views/system/manageuser.jsp");
            view.forward(request, response);
        }

    else {
            PrintWriter out = response.getWriter();
            out.print("Served at: " + request.getContextPath());
            RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/views/system/index.jsp");
            view.forward(request, response);
        }
    }

我想使用Model和view将上面的doGET转换为Spring RequestMapping 实施例

@RequestMapping(value ="/grcon" ,method = RequestMethod.GET)
    public ModelAndView getGrcon()

        ModelAndView modegeron = new ModelAndView("index");

if (path != null && path.equals("register")) {
      view = request.getRequestDispatcher("/WEB-INF/views/system/registeruser.jsp");

        return modegeron;

    }
}

2 个答案:

答案 0 :(得分:1)

你正在复杂化,利用弹簧的力量来处理复杂性。

首先为您的配置添加InternalResourceViewResolver,这将处理资源转换视图。

<bean id="viewResolver" class="InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/system/" />
    <property name="suffix" value=".jsp" />
</bean>

接下来,在您的params中添加@RequestMapping元素,以进一步说明映射。

@RequestMapping(value ="/grcon", method = RequestMethod.GET, params="path=register")
public String register() {
    return "registeruser";
}

@RequestMapping(value ="/grcon", method = RequestMethod.GET, params="path= usermang")
public String manageUser() {
    return "manageuser";
}

@RequestMapping(value ="/grcon", method = RequestMethod.GET)
public String index() {
    return "index";
}

或者使用具有@RequestParam的方法并返回适当的视图。在进行String比较时,小提示会将静态值放在第一位,为您节省null次检查。

@RequestMapping(value ="/grcon", method = RequestMethod.GET)
public String register(@RequestParam(value="path", required=false) String path) {
    if ("register".equals(path) ) {
        return "registeruser";
    } else if ("usermang".equals(path)) {
        return "manageuser";
    }
    return "index";
}

但一般情况下,您还需要对Model做一些准备,以便第一个可能更适用。

答案 1 :(得分:0)

@RequestMapping(value ="/grcon/{path}" ,method = RequestMethod.GET)
public ModelAndView getGrcon(@PathParam String path)

    ModelAndView modegeron = null;

    if (path != null && path.equals("register")) {
       modegeron = new ModelAndView("registeruser");
    } else if (path != null && path.equals("usermang") {
       modegeron = new ModelAndView("manageuser");
    } else {
       modegeron = new ModelAndView("index");
    }

    return modegeron;
}