Spring MVC学习笔记(一)配置文件
2020-06-28 04:38
330 查看
SpringMVC基本运行
首先我们要知道在SpringMvc中有一个Front controller,整个流程将以它为中心即
DispatcherServlet,它是一个实际的
Servlet(它继承自
HttpServletbase class),因此在 web application 中声明。整个流程就是客户端发起请求,由
SpringMvc中的
DispatcherServlet寻找对应的控制器进行处理,处理之后会返回一个数据模型,然后再由
DispatcherServlet将这个数据模型交给视图解析器进行渲染,渲染完毕返回给
DispatcherServlet,然后
DispatcherServlet将渲染好的视图返回给客户端。[如有不正确,请指出,谢谢]
web.xml配置
DispatchServlet
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMvcWebConfig</display-name> <!-- 解决编码问题 --> <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> <!-- 配置rootApplicationContext,这里写了一个空的,可要可不要 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>Root-servlet.xml</param-value> </context-param> <!-- 在初始化DispatcherServlet时, 默认Spring MVC 在 web application 的WEB-INF目录中 查找名为 [servlet-name]-servlet.xml 的文件(此处为SpringMvc-servlet.xml),并创建在那里定义的 beans, 覆盖 global 范围内使用相同 name 定义的任何 beans 的定义。 --> <servlet> <servlet-name>SpringMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- contextConfigLocation 名称固定,这是DispatcherServlet的一个属性--> <param-name>contextConfigLocation</param-name> <!-- 自定义路径和名称 放到conf下--> <param-value>classpath:SpringMvc.xml</param-value> </init-param> <!-- 启动servlet的优先级(多个servlet时越小越先启动) --> <load-on-startup>1</load-on-startup> </servlet> <!-- 请求路径映射 --> <servlet-mapping> <servlet-name>SpringMvc</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
也可以使用JavaConfig
package config.one; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; /** * 功能等价于web.xml * * @author ZM * */ public class MDispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { SpringConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { SpringMvcConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } }
类似的用于配置DispatcherServlet的文件可以有多个 此处咱没用到,可不看,看单个的DispatcherServlet
,但是在讨论SpringMvc容器和Spring容器之间的关系时,你将会发现整合他俩之后,Spring容器将会是SpringMvc的父容器,你也可以申明一个RootConfig.xml用于共享多个DispatcherServlet之间的信息。他们的关系如下图
我们这里考虑为单个
DispatcherServlet
那我们接下来就需要配置
webApplicationContext即
SpringMvc-servlet.xml,此配置文件将包含所有 Spring Web MVC-specific 组件(beans),也就是用来注册所需要的bean
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 定义一个视图资源解析器,在@RequestMapping标注的方法返回一个视图名,这个资源解析器会得到这样一个视图 prefix+视图名+suffix --> <context:component-scan base-package="controller"></context:component-scan> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
也可以基于JavaConfig
package config.one; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** * 类似Spring中的配置文件 * * @author ZM * */ @Configuration @ComponentScan(value = { "config", "com" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, classes = { EnableWebMvc.class }) }) public class SpringConfig { }
package config.one; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; /** * 功能等价于springmvc-servlet.xml 也就是Spring中的applicationContext.xml * * */ @Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.zj.springmvc.one" }) public class SpringMvcConfig extends WebMvcConfigurerAdapter { /** * 视图解析器 * * @return */ @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/view/"); // 前缀 resolver.setSuffix(".jsp");// 后缀 resolver.setExposeContextBeansAsAttributes(true); return resolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable();// 页面静态化处理 } }
测试文件:
Test.java
package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class Test { @RequestMapping("/test") public String test() { return "test"; } @RequestMapping("/success") public String success(@RequestParam("input") String input) { System.out.println(input); return "success"; } }
test.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <form action="success"> 输入输出看汉字是否正确<input name="input"> </form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <h1>成功</h1> </body> </html>
相关文章推荐
- spring MVC学习笔记(二) springMVC.xml配置文件解析
- spring mvc学习笔记(一)web.xml文件配置的一点重要信息
- java学习笔记7 - Spring mvc 统一异常处理和静态文件的配置
- hibernate3 学习笔记(三) hibernate 日志配置文件 log4j.properties
- lamp学习笔记之一-----apache配置文件详解
- spring-security学习笔记--配置文件
- Unity Application Block 学习笔记之一使用配置文件
- Linux学习笔记1—Linux文件权限及目录配置
- Hibernate配置文件属性学习笔记
- spring学习笔记:spring mvc不得不说的几处配置(转自:http://blog.csdn.net/sunxing007/article/details/4584300)
- spring学习笔记:spring mvc不得不说的几处配置
- nginx 源码学习笔记(十三)——文件读写和配置文件读取
- Struts 2 学习笔记 - 4.配置文件深入(2):action,result,exception
- DCMTK开源库的学习笔记4:利用ini配置文件对dcm影像进行归档
- nginx 学习笔记(6) nginx配置文件中的度量单位
- spring-security学习笔记--配置文件
- SaltStack 学习笔记 - 第三篇: Salt-Minion配置文件详解
- 【转】VIM学习笔记&nbsp;配置文件(…
- [学习笔记]将LinqToSql的连接字符串写在配置文件中
- Autofac学习笔记之-配置文件实现组件注册