一、导入spring所需jar包.
由于是在内网,所以使用楼下的maven仓库。
查找以下jar的pom 段
spring-aop
spring-asm(aop依赖asm实现动态代理)
spring-beans
spring-context
spring-core
spring-web
spring-tx
spring-context-support
spring-webmvc
二、配置web.xml
定义dispatcherServlet ,用于拦截请求。
三、建立Controller
在controller中添加RequestMapping注解,便于未来进行权限控制。
创建spring-servlet.cml
配置包扫描。
<context:component-scan base-package="com.cyou.nad.*"use-default-filters="false" >
<context:include-filtertype="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
use-default-filters="true" 表示扫描所有注解
use-default-filters="false"表示只扫描nclude-filter中配置的注解。
使用tomcat运行工程:
出现class not found 问题,在tomcat的部署文件夹中没有发现相应jar包。
原因:未将maven引入的jar加入build path
maven install时出现需要1.4以上版本jre的问题。
原因:Preferences中java-install jres配置有问题。
修改配置,使其指向安装的jdk路径。
四、配置视图
添加视图解析器,配置location,确定从哪个目录查找视图。
使用tomcat运行工程,出现500错误。
原因:缺少freemarker 的jar包。
五、配置springcontext,建立service和dao
配置监听web容器启动,加载spring。
配置applicationContext.xml (用于管理spring)
<context:component-scan base-package="com.cyou.nad.*"use-default-filters="true" >
<context:exclude-filtertype="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
为了防止二次扫描controller,所以使用exclude-filter,使其不再扫描controller。
创建service,使用@service,出现不能找到相应jar的问题。
原因:未给service工程配置pom文件。
在dao中使用@Repository注解,标记此类为dao
六、如何配置将静态资源与其他资源分离
在spring-servlet.xml中配置
<mvc:default-servlet-handler/>(速度较慢,需要遍历寻找)
或者
<mvc:resource mapping="/img/**"location="/img/"/>(配置较多,对于每一种静态资源都需要单独配置)
…
本文详细介绍如何使用Spring MVC框架搭建Web应用程序,包括导入所需jar包、配置web.xml、创建Controller及Service层等内容,并解决常见问题。
754

被折叠的 条评论
为什么被折叠?



