标签:
1.从官网下载spring相关jar http://spring.io/projects 点击SPRING FRAMEWORK
2.从GitHtb下载
3.向下滚动找到Downloading Artifacts 点击
4.点击Artifacts--》libs-release-local--》org--》springframewor--》spring
5.选中一个自己需要的版本(我选了4.2.5)
6.下载后的文件
配置文件的规范约束下面文件夹有的都可以写(我用到了beans, context, mvc三个, 如果需要配置数据源的可以加jdbc)
7.创建项目并配置环境
下面是贴代码:
applicationContext.xml (默认在WEB-INFO/applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <!-- 目前只是使用spring的web-mvc功能 数据源切面整合持久化框架可在此配置 --> </beans>
spring-webmvc-dispatcher-servlet.xml (默认为DispacherServlet-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:context="http://www.springframework.org/schema/context" ##自动扫描 xmlns:mvc="http://www.springframework.org/schema/mvc" ##静态资源处理 xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!-- 自动扫描的包名 --> <context:component-scan base-package="cn.zjx.controller"/> <bean id="annotationUrlHandlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!-- 视图解释类 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 默认的注解映射的支持 加<mvc:resources...前,要加上<mvc:annotation-driver/>,否则servlet控件器会解析失败 --> <mvc:annotation-driven /> <!-- 对静态资源文件的访问 方案一 (二选一) --> <!-- <mvc:default-servlet-handler/> --> <!-- 对静态资源文件的访问 方案二 (二选一)cache-period 缓存周期--> <mvc:resources mapping="/resource/js/**" location="/resource/js/" cache-period="31556926"/> <mvc:resources mapping="/resource/css/**" location="/resource/css/" cache-period="31556926"/> <mvc:resources mapping="/resource/images/**" location="/resource/images/" cache-period="31556926"/> </beans>
对静态资源文件的访问的其它方式:
交由上级web容器处理,在web.xml中的org.springframework.web.servlet.DispatcherServlet 配置之前加入如下代码:
<servlet-mapping>
<servlet-name>
default
</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
根据不同的资源文件制定不同的映射机制,其中servlet-name在tomcat中默认是default,
该servlet在tomcat\config\web.xml文件中默认配置,不同的应用服务器对应的静态资源文件处理的servlet名称不尽相同,如下:
tomcat,jboss,glassfish,jetty : defaut;
weblogic : FileServlet;
websphere : SimpleFileServlet;
resin : resin-file;
GAE : _ah_default
web.xml
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>zjx-springmvc</display-name> <!-- 如果WEB-INFO下没有application.xml或者不叫这个名需要通过contex-param来加载 --> <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/configs.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> <!-- 如果WEB-INF 包含文件名称为 DispatcherServlet-servlet.xml(默认找DispatcherServlet-servlet.xml这个文件) 则不需要在inint配置命名空间来加载该***-servlet.xml 文件 --> <init-param> <param-name>namespace</param-name> <param-value>spring-webmvc-dispatcher-servlet</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
TestController.java
package cn.zjx.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class TestController { @RequestMapping(value="/insert") public String save(){ System.out.println("insert success"); return "user_manager"; } @RequestMapping(value="/selectList") public String selectList(HttpServletRequest requset){
request.setAttribute("size", "5个用户"); return "user_manager"; } }
8.启动tomcat 可以看到加载顺序与加载的文件以及实例化的testController对象及方法的url映射名称和静态资源映射;
如下:加粗字体
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
五月 08, 2016 3:47:21 上午 org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 1932 ms
五月 08, 2016 3:47:22 上午 org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet ‘DispatcherServlet‘
五月 08, 2016 3:47:22 上午 org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet ‘DispatcherServlet‘: initialization started
五月 08, 2016 3:47:22 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace ‘spring-webmvc-dispatcher-servlet‘: startup date [Sun May 08 03:47:22 CST 2016]; parent: Root WebApplicationContext
五月 08, 2016 3:47:22 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-webmvc-dispatcher-servlet.xml]
五月 08, 2016 3:47:25 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/insert] onto handler ‘testController‘
五月 08, 2016 3:47:25 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/insert.*] onto handler ‘testController‘
五月 08, 2016 3:47:25 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/insert/] onto handler ‘testController‘
五月 08, 2016 3:47:25 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/selectList] onto handler ‘testController‘
五月 08, 2016 3:47:25 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/selectList.*] onto handler ‘testController‘
五月 08, 2016 3:47:25 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/selectList/] onto handler ‘testController‘
五月 08, 2016 3:47:26 上午 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry register
INFO: Mapped "{[/insert]}" onto public java.lang.String cn.zjx.controller.TestController.save()
五月 08, 2016 3:47:26 上午 org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry register
INFO: Mapped "{[/selectList]}" onto public java.lang.String cn.zjx.controller.TestController.selectList(javax.servlet.http.HttpServletRequest)
五月 08, 2016 3:47:26 上午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace ‘spring-webmvc-dispatcher-servlet‘: startup date [Sun May 08 03:47:22 CST 2016]; parent: Root WebApplicationContext
五月 08, 2016 3:47:27 上午 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace ‘spring-webmvc-dispatcher-servlet‘: startup date [Sun May 08 03:47:22 CST 2016]; parent: Root WebApplicationContext
五月 08, 2016 3:47:27 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/resource/js/**] onto handler ‘org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0‘
五月 08, 2016 3:47:27 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/resource/css/**] onto handler ‘org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1‘
五月 08, 2016 3:47:27 上午 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/resource/images/**] onto handler ‘org.springframework.web.servlet.resource.ResourceHttpRequestHandler#2‘
五月 08, 2016 3:47:27 上午 org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet ‘DispatcherServlet‘: initialization completed in 5507 ms
五月 08, 2016 3:47:27 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-6661"]
五月 08, 2016 3:47:27 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-6666"]
五月 08, 2016 3:47:27 上午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 16614 ms
静态资源可以访问
虽然web.xml中url-pattern为/
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>namespace</param-name>
<param-value>spring-webmvc-dispatcher-servlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
但是有它:
<mvc:resources mapping="/resource/images/**" location="/resource/images/" cache-period="31556926"/>
测试insert方法
http://localhost:6661/zjx-springmvc/insert
测试selectList查询方法
http://localhost:6661/zjx-springmvc/selectList
--------------------------------------------------------------------------------------------------
先到这里
接下来是配置velocity和log4j日志配置Dobble
然后通过maven命令来生成分布式项目结构如下结构把架构移植到下面就是spring-mvc为控制器的分布式架构了\(^o^)/~
--------------等待更新--------------------
spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描
标签:
原文地址:http://www.cnblogs.com/YingYue/p/5469686.html