springmvc
mvc: 模型()
1.1 回顾servlet
servlet:
转发与重定向
1.2 spring-annotation总结
@Controller
@RequestMapping(value=“user”, method=)
@GetMapping
@PostMapping
@ResponseBody
@PathVariable 在变量上用
1.3 spring-controller
@Controller注解,把类交给spring托管
@RequestMapping("/h1")
public String hello(Model model){
model.addAttribute("msg","hello, controller");
return "hello"; //返回字符串,跟视图解析起中的前缀和后缀做拼接
}
1.4 关于@RequestMapping(“abc”)
这个注解可以放在类上面,也可以放在方法上面。
它会将项目路径和上面字符串拼接好
1.5 Restful风格
可以通过不同的请求方式来实现不同的效果,请求地址一样,但可以实现功能不一样;
主要是看请求方式是:GET、Request、Delete、Put
Restful风格可以隐藏请求的格式
1.6 不经过视图解析器,直接用原生的servlet跳转
@Controller
public class ModelTest {
@RequestMapping("/m1/t1")
public String test1(Model model){
// model
return "redirect:/index.jsp"; //用forward直接转发
}
}
1.7 springmvc处理数据的方式
1 接受前台数据,可以用@PathVariable
@RequestMapping(value="/add/{a}/{b}",method = RequestMethod.GET)
public String test(@PathVariable int a,@PathVariable int b, Model model){
int res = a + b;
model.addAttribute("msg",res);
return "hello";
}
@Component
@Service
@Controller
@Repository
依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- json-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
</dependencies>
web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispahterServlet绑定springmvc配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.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>
关于spring-mvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.example.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!-- 支持mvc注解驱动:
在spring中一般采用@RequestMapping注解来完成映射关系,要想使用@RequestMapping注解生效,必须向上下文中
注册DefaultAnnotationHandlerMapping和一个AnnotationMethodHandlerAdapter实例
这两个实例分别在类别和方法级别处理
而annotation-driver配置帮助我们自动完成两个实例的注入-->
<!-- 过滤静态资源-->
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
总结
spring-mvc就是封装类请求,在请求与servlet直接接触中间加一个DispatherServlet,然后
让中间层去做分发,这个类在org.springframework:spring-webmvc下
它的处理流程是这样的
前端请求过来—>
参考文献:
https://blog.csdn.net/weixin_44635198/article/details/107444925