SpringMVC: 结果跳转方式
ModelAndView
设置 ModelAndView 对象 , 根据 view 的名称 , 和视图解析器调到指定的页面
第一种跳转方式
页面: {视图解析器前缀} + viewName + {视图解析器后缀}
<!--视图解析器: 模板引擎 Thymeleaf Freemarker-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>对应的 controller 类
public class ControllerTest1 implements Controller{
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletRequest request) {
//返回一个模型视图对象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
第二种跳转方式
通过 设置 ServletAPI,不需要视图解析器
package cn.itcast.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class Login {
@RequestMapping("/login")
public String login(String username, String password, HttpServletRequest request) {
System.out.println("用户名:"+username+"\t密码:"+password);
request.setAttribute("username", username);
request.setAttribute("password", password);
if(username.equals("root1") && password.equals("root1")){
return "success";
}else{
return "register";
}
}
}