Spring MVC (mvc框架)

与struts2属于竞争关系,是MVC框架。

1.依赖

<dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>4.0.6.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>4.0.6.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
		<version>4.0.6.RELEASE</version>
	</dependency>
</dependencies>

2.配置

2.1 web.xml

web.xml模板,让tomcat容器根据请求转发到springmvc dispatcher。
<?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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- specify the exact path -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/springmvc-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/springmvc/*</url-pattern>
	</servlet-mapping>
</web-app>

2.2 springmvc-config

告诉mvc框架,有哪些controller。推荐基于注解的方式,这样在配置文件中只要指定去哪里扫描含有注解的类就行了。
默认DispatcherServlet会加载 WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件。
 <!--XXX-servlet.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:p="http://www.springframework.org/schema/p"
	xmlns:lang="http://www.springframework.org/schema/lang" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	         http://www.springframework.org/schema/beans/spring-beans.xsd
	         http://www.springframework.org/schema/lang
	         http://www.springframework.org/schema/lang/spring-lang.xsd      
	         http://www.springframework.org/schema/tx   
	         http://www.springframework.org/schema/tx/spring-tx.xsd    
	         http://www.springframework.org/schema/aop     
	         http://www.springframework.org/schema/aop/spring-aop.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
	         http://www.springframework.org/schema/cache
	         http://www.springframework.org/schema/cache/spring-cache.xsd
	         http://www.springframework.org/schema/task 
	         http://www.springframework.org/schema/task/spring-task.xsd">
	
	<context:annotation-config />
	<!-- 自动扫描指定包及其子包下的所有Bean类 -->
	<context:component-scan 
		base-package="com.yichudu.springmvc"/>
</beans>

2.3ApplicationContext.xml

ContextLoaderListener在启动Web容器时,自动装配/WEB-INF/ApplicationContext.xml文件中的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。

3.相关注解

3.1 路由相关

@ Controller
请求总要落到某个controller上处理。
@ RequestMapping
可以加在类上,也可以加在方法上。value参数表明path,method参数表示方法。

3.2 请求参数注入

@ RequestParam
对应url中的query参数。

3.3 指定返回类型

@ ResponseBody

4.感知ApplicationContext

除了让spring主动注入,我们也可以在代码中拿到context,随心所欲地拿bean,更灵活。
@Component
public class ContextUtil implements org.springframework.context.ApplicationContextAware{
    public static ApplicationContext context = null ;
    @Override
    public void setApplicationContext(ApplicationContext appcontext) throws BeansException {
            context = appcontext ;
    }
}

5.常见问答

:各个path之间的关系是什么?
:第一层——webapp的目录名;第二层——servlet的url-pattern;第三层——controller类上的RequestMapping;第四层——controller类中方法上的RequestMapping。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值