springmvc入门demo说明

来源:互联网 发布:js 字符串转date 编辑:程序博客网 时间:2024/06/06 11:01


刚才写了一个springmvc的入门demo(http://blog.csdn.net/baikh1612/article/details/8038637),现在做下简单的说明.

springmvc采用servlet做一个前端控制器,web.xml如下

<servlet><!-- 如果这样配制必须在WEB-INF目录下有一个helloWorld-servlet.xml文件可以通过以下配制改变默认的配制<init-param><param-name>contextConfigLocation</param-name><param-value>xml文件路径</param-value></init-param>以下配制初化一个helloWorld的ApplicationContext --><servlet-name>helloWorld</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>helloWorld</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

1.url-pattern为/表示处理所有的请求,所以当请求一个css,js文件时,也会被springmvc所拦截,会出现404错误,需要在helloWorld-servlet.xml中配制<resource>,使他不受mvc拦截.

很多人喜欢配制为*.do,这样一来如你的RequestMapping("hello"),需要请求hello.do.

2.当我们用spring集成其他mvc框架,如(struts,jsf)时,经常需要在web.xml文件中做如下配制,用来加载启动spring的ApplicationContext

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>

在springmvc也可以配制上面的代码,springmvc首先会根据DispatcherServlet启动一个名为<servlet-name>的ApplicationContext,然后再根据ContextLoaderListener启动一个root的ApplicationContext,serlet-name的ApplicationContext可以访问root ApplicationContext中定义的Bean.


3.hello-servlet.xml配制.这个文件会被DispatcherServlet加载,启动生成servlet-name>的ApplicationContext,之所以过滤掉Service,Repository的bean,这里的目的只让它扫描加载@Controller定义的Bean.一般情况下我更喜欢把@Service,@Repository的spring bean定义在root的applicationContext(可以查看applicationContext.xml,通常要配制事务),这个spring文件只配制mvc相关的东西,如配制了一个viewResolver,作用是根据返回的逻辑视图名如:hello,映射到实际的文件路径,如/web-inf/jsp/hello.jsp.

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="com.study.springmvc.controller"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Service" /><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Repository" /></context:component-scan><bean id="viewResolver"class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>
rootApplicationContext加载的文件applicationContext.xml,与上面的文件相反加载@Service,@Repository,过滤@Controller,通常与一些orm框架集成时,要配制事务,事务配制在这个文件中,不需要配制在上面的文件,如果hello-servlet.xml扫描到@Service,@Repository定义的Bean,而在hello-servlet.xml没有配制事务,则出出现transaction required的异常.

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="com.study.springmvc"><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /></context:component-scan></beans>