目录
一、Controller
1、编写一个测试类
public class TestController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","hello,Controller2");
mv.setViewName("test");
return mv;
}
}
2、SpringMVC配置文件中注册请求的bean
<bean name="/t" class="com.jia.controller.TestController"/>
3、删除处理器映射器、处理器适配器配置
注销掉
4、测试
- 实现接口Controller定义控制器是较老的办法
- 缺点是:一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller;定义的方式比 较麻烦;
二、使用注解Controller
1、添加注解扫描
Spring可以使用扫描机制来找到应用程序中所有基于注解的控制器类
<!--自动扫描包,让指定包下得注解生效,有IOC同一管理-->
<context:component-scan base-package="com.jia.controller"/>
2、编写注解实现类
//@Controller注解的类会自动添加到Spring上下文中
@Controller
public class TestController2 {
//映射访问路径
@RequestMapping("/c")
public String test(Model model){
//Spring MVC会自动实例化一个Model对象用于向视图中传值
model.addAttribute("msg","hello,注解的Controller");
//返回视图位置
return "hello";
}
}
3、测试
三、RequestMapping
@RequestMapping
注解用于映射
url
到控制器类或一个特定的处理程序方法。可用于类或方法上。
用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
//映射访问路径
@RequestMapping(value = "/hello") //匹配所有请求的/hello
------------------------------------------------------------------------
@RequestMapping(value = "/hello",method = RequestMethod.GET) //只匹配Get请求的/hello
------------------------------------------------------------------------
@GetMapping(value = "/hello") //只匹配Get请求的/hello
------------------------------------------------------------------------
@PostMapping(value = "/hello") //只匹配Post请求的/hello
------------------------------------------------------------------------
四、结果跳转方式
1、ModelAndView
- 设置ModelAndView对象 , 根据view的名称 , 和视图解析器跳到指定的页面 .
- 页面 : {视图解析器前缀} + viewName +{视图解析器后缀}
<!--视图解析器-->
<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 TestMV {
@RequestMapping("/mv")
public ModelAndView test(){
ModelAndView mv = new ModelAndView();
mv.addObject("msg","hello,ModelAndView"); //封装数据
mv.setViewName("hello");//设置跳转
return mv;
}
}
2、ServletAPI
通过设置ServletAPI , 不需要视图解析器
转发
@Controller
public class TestServlet {
@RequestMapping("/s")
public void test(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
request.setAttribute("msg","Hello,Servlet转发");
request.getRequestDispatcher("WEB-INF/jsp/hello.jsp").forward(request,response);
}
}
重定向
(转发不需要加项目名,重定向需要加项目名)
@Controller
public class TestServlet {
@RequestMapping("/s")
public void test(HttpServletRequest request, HttpServletResponse response) {
System.out.println("______________");
try {
response.sendRedirect("/springmvctest02_war_exploded/index.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、SpringMVC
转发(不带视图解析器)
@Controller
public class TestSpringMVC {
@RequestMapping("/mvc")
public String test(HttpServletRequest request){
request.setAttribute("msg","使用SpringMVC实现转发");
return "/WEB-INF/jsp/hello.jsp";
}
}
@Controller
public class TestSpringMVC {
@RequestMapping("/mvc")
public String test(HttpServletRequest request){
request.setAttribute("msg","使用SpringMVC实现转发");
return "forward:/WEB-INF/jsp/hello.jsp";//显示定义为转发
}
}
重定向(不带视图解析器)
@Controller
public class TestSpringMVC {
@RequestMapping("/mvc")
public String test(){
return "redirect:/jsp/hello.jsp";
}
}
转发和重定向 - 有视图解析器;
@Controller
public class TestSpringMVC {
@RequestMapping("/mvc")
public String test(){
return "test";
}
@RequestMapping("/mvc2")
public String test2(){
return "redirect:index.jsp";
}
}