张店网站建设哪家好,wordpress加密文章,淘宝优惠券返利网站怎么做,wordpress照相馆主题简介
Spring MVC 属于 SpringFrameWork 的后续产品#xff0c;已经融合在 Spring Web Flow 里面#xff1b;Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块#xff1b;使用 Spring 可插入的 MVC 架构#xff0c;从而在使用Spring进行WEB开发时#xff0c;可以选择…简介
Spring MVC 属于 SpringFrameWork 的后续产品已经融合在 Spring Web Flow 里面Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块使用 Spring 可插入的 MVC 架构从而在使用Spring进行WEB开发时可以选择使用 Spring的SpringMVC 框架或集成其他MVC开发框架下面将演示搭建第一个 SpringMVC 项目
实现步骤 首先我们先创建一个动态 web 项目名为SpringMVC如果不用 maven 的话Spring 的对应 jar 包可以直接在这个网址下载https://repo.spring.io/libs-release-local/org/springframework/spring/ 将 Spring 的包全部导入到项目的 lib 文件夹下除了 Spring 的包之外还有一个 commons-logging.jar 包一样要导入进去 在 web.xml 中添加如下内容注意这里的 servlet-classorg.springframework.web.servlet.DispatcherServlet我们要使用 Spring 的DispatcherServlet 来控制流程拦截项目中其他的 xml 文件
servletservlet-namespring/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/spring-servlet.xml/param-value/init-paramload-on-startup1/load-on-startup
/servlet
servlet-mappingservlet-namespring/servlet-nameurl-pattern//url-pattern
/servlet-mapping还可以顺便加上中文过滤器
!-- 字符过滤器 --
filterfilter-nameCharacterFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueUTF-8/param-value/init-param
/filter
filter-mappingfilter-nameCharacterFilter/filter-nameurl-pattern/*/url-pattern
/filter-mapping在 web.xml 同目录下创建一个 spring-servlet.xml 文件 给 spring-servlet.xml 文件添加相应的 schema 配置, 可以通过打开 \docs\spring-framework-reference\htmlsingle.html 文件然后搜索‘xmlns:mvc’ 找到相应的 schema注意还要添加 context 的 schema最基本的内容如下
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:mvchttp://www.springframework.org/schema/mvcxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd配置如下内容context:component-scan是指程序会在哪些包下面去找添加了 annotation 的类 mvc:annotation-driven/ 是指打开 SpringMVC的annotation功能最后的 beanInternalResourceViewResolver 是指我们选择这种方式来映射 view里面的两个配置分别是返回映射的前缀和后缀假如在controller 中返回了 ‘hello’ 字符串那么view的路径就是 view path prefix ‘hello’ ‘.jsp’
context:component-scan base-packagecom.ibm.reskill/
mvc:annotation-driven/
!--(推荐)第一种视图层配置 --
bean classorg.springframework.web.servlet.view.InternalResourceViewResolver! -- 可省略 --property nameviewClass valueorg.springframework.web.servlet.view.JstlView /property nameprefix value/WEB-INF/jsp//property namesuffix value.jsp/
/bean!--第二种视图层配置 --
bean idviewResolver classorg.springframework.web.servlet.view.UrlBasedViewResolver! -- 可省略 --property nameviewClass valueorg.springframework.web.servlet.view.JstlView /property nameprefix value/WEB-INF/jsp/ /property namesuffix value.jsp /
/bean7.新建一个controller.class来测试 Controller(testController)Scope(singleton) //单例模式默认可省略多例模式的话应配置成 prototype
public class TestController {RequestMapping({/hello, /})public String hello() {System.out.println(hello);return hello;}
}注意如果按照以上步骤操作出现404错误并发现 nohandlerfound 异常
(1) 仔细检查每一个配置文件中的配置内容是否正确
(2) 如果确定每一个配置文件正确引用的class也没有问题那么可以尝试在 eclipse 中手动 bulid project