SpringMVC 跳转到不同的jsp目录

SpringMVC的控制层的分发器(Controller注解)通过返回view,然后结合视图处理器(viewResolver),最终跳转到指定目录的jsp。如果想在Controller的业务方法中跳转到不同的目录,例如跳转到error中error.jsp中。本文主要介绍SpringMVC在Controller的业务方法中如何跳转到不同的jsp目录。
SpringMVC 跳转到不同的jsp目录_第1张图片

spring.xml


    
        
        
    

controller层

package com.kmyang.project.user.controll;

import com.kmyang.project.common.exception.UnknowAccountException;
import com.kmyang.project.common.exception.WrongPasswordException;
import com.kmyang.project.hotel.beans.TbHotel;
import com.kmyang.project.hotel.service.IHotelService;
import com.kmyang.project.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IHotelService hotelService;
    
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String username, String password, Model model){
        String page = "page/index";
        try {
            userService.login(username,password);
            List hotels = hotelService.queryHotelList(1);
            model.addAttribute("hotels", hotels);
            model.addAttribute("name", username);
        } catch (NullPointerException e) {
            e.printStackTrace();
            page= "error/nullPointer";
        } catch (UnknowAccountException e) {
            e.printStackTrace();
            page= "error/unknowAccount";
        } catch (WrongPasswordException e) {
            e.printStackTrace();
            page= "error/wrongPassword";
        } catch (Exception e) {
            e.printStackTrace();
            page= "error/error";
        }
        return page;
    }
}

原理:在Controller层的业务方法中指定"跳转的目录"+“目标JSP名称”。

你可能感兴趣的:(IDEA,springMVC+jsp)