SpringMVC基础知识
学完SpringMVC必须知道的知识点,
首先导依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
- web与spring的集成
web.xml的配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml文件内容
<bean class="com.pojo.User" id="user">
<property name="name" value="FreeMan"/>
<property name="sex" value="Man"/>
</bean>
使用以上web.xml配置把spring的配置文件在服务器启动时就直接加载进去,不用我们手动加载spring配置文件
测试:spring是否加载成功
@WebServlet("/User")
public class UserController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
//使用就监听器让服务器一启动就创建应用上下文,保存在最大域Context中 避免每次都需要创建应用上下文
ServletContext servletContext = this.getServletContext();
ApplicationContext app=WebApplicationContextUtils.getWebApplicationContext(servletContext);
User bean = app.getBean(User.class);
System.out.println(bean);
}
}
启动服务器输入配置的Servlet地址回来查看控制台显示如下
这样就spring与web集成了
- SpringMVC配置
配置 web.xml文件,把springmvc-config.xml 加载到核心控制器(配置前端控制器)
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<!-- 配置局部变量加载mvc配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
控制器
@Controller
@RequestMapping("/user")//访问路径的总地址
public class UserController {
@RequestMapping(value = "/test1" ,method = RequestMethod.GET)//uesr路径下的test1路径
public String test1(){
System.out.println("字符串返回方法代表能返回页面");
return "../success.jsp";//字符串返回的是一个页面地址
}
}
利用web.xml文件添加过滤器防止中文乱码:
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!--声明局部变量 把编码改为 utf-8-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样web.xml的东西差不多就配好了
1.加载应用上下文(加载 applicationContext.xml文件)
2.配置前端控制器(加载springmvc-config.xml文件)
3.配置过滤器(防止中文乱码)
web.xml配置代码:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 配置全局过滤-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置框架自动创建应用上下文-->
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc-config.xml基础配置
第一步配置注解扫描,只扫描控制层
<context:component-scan base-package="com.controller"/>
第二步 配置注解驱动
<mvc:annotation-driven/>
第三步开放资源权限
<mvc:default-servlet-handler/>
第四步配置视图解析器
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
未配置视图解析器 返回的路径前+../
返回到根路径前端控制器才能找到
基本的springmvc-config.xml的文件配置就差不多了
合在一起代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--默认资源开放-->
<mvc:default-servlet-handler/>
<!--配置注解扫描 mvc只扫描控制层 其余层由 spring扫描-->
<context:component-scan base-package="com.controller">
</context:component-scan>
<!-- 注解驱动-->
<mvc:annotation-driven/>
<!-- 视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 控制层的跳转方法
1.字符串方法跳转
@RequestMapping(value = "/test1" ,method = RequestMethod.GET)
public String test1(){
System.out.println("字符串返回方法代表能返回页面");
return "success";
}
2 返回modelandView方法
@RequestMapping(value = "/test2" ,method = RequestMethod.GET)
public ModelAndView test2(){
System.out.println("字符串返回方法代表能返回页面");
ModelAndView modelAndView=new ModelAndView();
// 存储值 相当于javaWeb的 session对象 利用键值存储
modelAndView.addObject("user",1111);
//设置跳转路径
modelAndView.setViewName("success");
return modelAndView;
}
3.形参类型自动注入
3.1 自动注入model 类型 ,返回字符串类型
@RequestMapping("/test3")
public String test3(Model model){
//自动把形参的类型注入
model.addAttribute("name","张三");
return "success";
}
3.2自动注入实体类型无返回值
网页地址上…/test5?name=zs&sex=1 进行访问 spring会自动把网页上传的参数与形参属性相同的进行注入
@RequestMapping("/test5")
// 告诉框架进行数据响应,不进行页面跳转
@ResponseBody
public void test5(User user){
System.out.println(user);
}
结果:
3.3
自动注入数组类型 ,只要网页参数名字与形参名字相同,且网页上传的参数有多个值,则会进行数组注入:输入网址…/test6/?str=1&str=2&str=3
@RequestMapping("/test6")
// 告诉框架进行数据响应,不进行页面跳转
@ResponseBody
public void test6(String str[]){
System.out.println(str);
}
3.4
网页上传的参数与 形参不一致也能进行赋值
使用@QuestParam进行对参数的映射
@RequestMapping("/test8")
// 告诉框架进行数据响应,不进行页面跳转
@ResponseBody
public void test8(@RequestParam(name = "sex") String ser){
System.out.println(Arrays.asList(ser));
}
restful风格编码根据提交方法不同,把参数跟着地址后面 而不是跟在问号后面不用带参数,直接地址后写/值
…/test1?name=2
restful风格:
…/test/2
@PathVariable()告诉Spring 是路径上的参数
@RequestMapping("/test9/{name}" )
// 告诉框架进行数据响应,不进行页面跳转
@ResponseBody
public void test9(@PathVariable ("name")String name){
System.out.println(name);
}
以上就是SpringMVC需要掌握的知识点
总结:
web.xml配置
三步:过滤器,加载应用上下文,配置前端控制器
springmvc-config配置
四步:包扫描,注解驱动,视图解析器,开放资源
controller控制层的跳转:字符串跳转页面,ModelAndView跳转
数据处理:形参自动注入,restful风格,引用类型注入
使用的注解:
@RequestMapp()
@Controller
@ResponseBody
@RequestBody
@PathVariable
@RequestParam