SSM-9-Spring_MVC
Spring-MVC的Hello World感觉好麻烦
Spring Mvc 工作流程
Spring MVC核心类
DispatchServlet
前端控制器作用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
|
这里发觉个很有意思的东西,因为他默认加载的配置文件为这个,然后我把哪里注释掉了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
|
意思他会按默认加载,然后我复制的另一份配置文件也是这个。。。复制错了,然后就死循环了,他一直在加载文件,应该复制的是springmvc-config而不是web.xml…
@Controller注解类型
注意这里有大坑,看视频新建项目他没有复制项目,而是文件一个个复制,因为复制项目无法运行,他会报错
context包扫描,就可以使用注解了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Controller public class FirstController{
@Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("msg", "这是我的第一个Spring MVC程序"); mav.setViewName("/WEB-INF/jsp/first.jsp"); return mav; }
}
|
RequestMapping
作为请求处理在接收到指定url响应
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Controller public class FirstController{
@RequestMapping(value="/firstController") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("msg", "这是我的第一个Spring MVC程序"); mav.setViewName("/WEB-INF/jsp/first.jsp"); return mav; }
}
|
还可以配置二级url
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Controller @RequestMapping(value="/hello") public class FirstController{
@RequestMapping(value="/firstController") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("msg", "这是我的第一个Spring MVC程序"); mav.setViewName("/WEB-INF/jsp/first.jsp"); return mav; }
}
|
属性
这个跟flask类似
name 别名,实际用处不大
method可以设置GET POST
params可选
value=”” value可以省略直接写””
组合注解
(value=””, method=Request.GET)类似于这种
请求处理方法的参数类型和返回类型
String返回无法带数据
使用添加Model
1 2 3 4 5 6 7 8 9
| @RequestMapping("/firstController") public String handleRequest(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception { ModelAndView mav = new ModelAndView(); model.addAttribute("msg", "这是我的第一个Spring MVC程序"); return "/WEB-INF/jsp/first.jsp"; }
|
- redirect
- forward
ViewResolver(视图解析器)
配置后可以简化操作
1 2 3 4 5
| <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean>
|
这时候只需返回first就行,因为前面后面已经加了
1 2 3 4 5 6 7 8 9 10 11
| @RequestMapping("/firstController") public String handleRequest(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception { ModelAndView mav = new ModelAndView(); model.addAttribute("msg", "这是我的第一个Spring MVC程序"); return "first"; } }
|
我这里配置文件复制错了,要ResourceViewResolver不能是ResourceView
基于注解的Spring MVC应用
本文作者:NoOne
本文地址: https://noonegroup.xyz/posts/e740e997/
版权声明:转载请注明出处!