一、首先,在配置前我们先来了解下springMVC基本概念
MVC的核心思想是业务数据抽取同业务数据呈现相分离。而SpringMVC运作图可以如下所示。
简单介绍个别元素作用:
HanderMapping :帮助DispatcherServlet获得正确的Controller, 将Controller用HanderInterceptor包裹起来 。
ModelAndView :是代表了MVC Web程序中Model与View的对象,不过它只是方便您一次返回这两个对象的holder, Model与View两者仍是分离的概念。可有ModelAndView、Model和Map类型。然而,ModelAndView ,Model,Map最终都会被DispatcherServlet转换为ModelAndView。
ViewResolver视图解析器:帮助DispatcherServlet处理的解析方法。
二、接下来开始进行配置。
(此在maven项目下进行配置,注意文件路径可能不同,若参考,请注意修改)
(1) 配置dispatcherServlet(可以理解为一个Servlet,配置他的配置文件路径以及配置文件)
配置文件:
①激活扫描注解
②DispatcherServlet上下文,只管理@Controller类型的bean,忽略其他型的bean,如@Service
③ 扩展注解驱动(更多的数据绑定等)
④ ViewResovler eg.前后缀或其他方式等
配置文件路径将在web.xml中配置
具体如下:
此将Controller文件放在controller包下,故此将扫描包赋值为controller base-packag="controller” 。
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 启用Spring基于annotation的DI, 使用户可以在Spring MVC中使用Spring的强大功能。 激活 @Required
@Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 等标注 -->
<context:annotation-config />
<!-- DispatcherServlet上下文, 只管理@Controller类型的bean, 忽略其他型的bean, 如@Service base-package为识别Controller注解的包 -->
<context:component-scan base-package="controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
<mvc:annotation-driven />
<!-- 静态资源处理, css, js, imgs -->
<!--<mvc:resources mapping="/resources/**" location="/resources/" />-->
<!-- 配置ViewResolver。 可以用多个ViewResolver。 使用order属性排序。 InternalResourceViewResolver放在最后。 -->
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="htm" value="text/html" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
(2)配置spring上下文 。
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--配置自动扫描的包 -->
<context:annotation-config/>
</beans>
(3)配置web.xml文件,配置中有详细注释,就不多说了。参考时请注意将dispatcherServlet文件路径以及spring上下文路径修改为自己。
<!--这样配置就可在此xml种使用EL表达式-->
<web-app version="3.0"
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">
<!-- 加载顺序 context-param -> listener -> filter -> servlet -->
<display-name>Spring MVC Study</display-name>
<!-- Spring应用上下文, 理解层次化的ApplicationContext -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/applicationContext.xml</param-value>
</context-param>
<!-- DispatcherServlet, Spring MVC的核心 -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/spring/web/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- mvc-dispatcher拦截所有的请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置完成后,再controller包下新建ControllerDemo
添加@Controller和@RequestMapping注解
@Controller
@RequestMapping("/baseDemo")
public class ControllerDemo {
@RequestMapping(value = "/baseType",method = RequestMethod.GET)
@ResponseBody// 返回json格式
// 当参数和url参数相同时,此处可不加@RequestParam("age")
public String baseType(@RequestParam("age") Integer age){
return "age: "+age;
}
}
启动tomcat,访问 http://localhost:8888/baseDemo/baseType?age=10
将会显示 age:10,则配置成功