三、maven项目搭建 springmvc+ spring + mybatis(springmvc搭建)
2017-08-23 18:15
656 查看
之前讲到maven搭建空白的web项目
现在来具体大奖 ssm 三层框架的搭建流程
现在网上有很多有关ssm的搭建教程 都是一次性搭建好的,我这里就将一下拆分讲解ssm框架
现在我们先来搭建springmvc这个控制层的框架
1、首先我们添加springmvc 的maven包,在pom.xml的 dependencies 内部添加
注意:这里的版本是4.0 的,用3.2版本的在jdk7 和 tomcat7可能会出现问题
本人遇到的遇到是使用ModelAndView 解析不到jsp文件,这里和网上经常说的包导错了的问题不是一个,应该是版本兼容性问题,这里不做深入讨论
2、添加相应的包名
![](https://img-blog.csdn.net/20170823182427892?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU291bEZlZWxpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3、添加spring-mvc.xml 的配置文件,在resources 下创建spring-mvc.xml 文件,并添加下面内容
这里只有三个功能:controller 控制层的扫描、静态资源的过滤、已经模型对象的前后缀添加
还有其他功能,比如说上传文件大小限制、json的支持、拦截器等功能
注意: 在beans 标签里面 的 xsi 中的 spring 对应的版本号一定好导入的版本号保持一致,否者会出现不可预料的问题:
比如4.0 的 写成3.2 出现配置开启静态资源访问后 正常访问的url 包404 错误 的问题
4、配置web.xml文件
在web.xml 中添加
5、创建Controller
在controller 包下创建 TestController.java
添加代码
添加对应的视图jsp文件,找到webapp在下面的WEB-INF添加 /view/test/index.jsp 文件夹和文件对应添加
index.jsp添加代码
<html>
<body>
<h2>Hello World! 来自: test.index的访问</h2>
</body>
</html>
保存后重启tomcat,然后访问 http://localhost:8080/SoulFeeling/test/index
显示Hello World! 即表示配置正常
现在来具体大奖 ssm 三层框架的搭建流程
现在网上有很多有关ssm的搭建教程 都是一次性搭建好的,我这里就将一下拆分讲解ssm框架
现在我们先来搭建springmvc这个控制层的框架
1、首先我们添加springmvc 的maven包,在pom.xml的 dependencies 内部添加
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.0.2.RELEASE</version> </dependency>
注意:这里的版本是4.0 的,用3.2版本的在jdk7 和 tomcat7可能会出现问题
本人遇到的遇到是使用ModelAndView 解析不到jsp文件,这里和网上经常说的包导错了的问题不是一个,应该是版本兼容性问题,这里不做深入讨论
2、添加相应的包名
3、添加spring-mvc.xml 的配置文件,在resources 下创建spring-mvc.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 扫描controller(controller层注入) --> <context:component-scan base-package="com.soul.ssm.controller"/>
<!-- 静态资源的过滤 --><mvc:resources location="/public/" mapping="/public/**"/> <!-- 对模型视图添加前后缀 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp"/> </beans>
这里只有三个功能:controller 控制层的扫描、静态资源的过滤、已经模型对象的前后缀添加
还有其他功能,比如说上传文件大小限制、json的支持、拦截器等功能
注意: 在beans 标签里面 的 xsi 中的 spring 对应的版本号一定好导入的版本号保持一致,否者会出现不可预料的问题:
比如4.0 的 写成3.2 出现配置开启静态资源访问后 正常访问的url 包404 错误 的问题
4、配置web.xml文件
在web.xml 中添加
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- springMVC核心配置 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--spingMVC的配置路径 --> <param-value>classpath:spring-mvc.xml</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>
5、创建Controller
在controller 包下创建 TestController.java
添加代码
package com.soul.ssm.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @RequestMapping("test") @Controller public class TestController { @RequestMapping("index") public ModelAndView index2(HttpServletRequest request, HttpServletResponse response) { System.out.println("测试2"); return new ModelAndView("/test/test"); } }
添加对应的视图jsp文件,找到webapp在下面的WEB-INF添加 /view/test/index.jsp 文件夹和文件对应添加
index.jsp添加代码
<html>
<body>
<h2>Hello World! 来自: test.index的访问</h2>
</body>
</html>
保存后重启tomcat,然后访问 http://localhost:8080/SoulFeeling/test/index
显示Hello World! 即表示配置正常
相关文章推荐
- 从零开始搭建maven+springmvc+mybatis web项目(3)---springmvc流程测试
- 从零开始搭建maven+springmvc+mybatis web项目(2)---springmvc配置文件编写
- springmvc+mybatis+redis的maven项目搭建
- 使用maven搭建SSM项目(spring+springmvc+Mybatis)
- Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建
- Maven搭建SpringMVC+Spring+Mybatis项目详解
- java搭建基于springboot的ssm(spring + springmvc + mybatis)的maven项目
- Eclipse+Tomcat+MAVEN+SVN项目完整环境搭建+SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 基于Maven项目,搭建Spring+SpringMVC+MyBatis的WEB项目(待更新)
- 使用myeclipse搭建带maven的ava web项目(Spring+SpringMVC+Mybatis)
- Maven搭建springMvc+myBatis完整项目
- spring SpringMVC MyBatis 搭建项目 MavenWeb项目
- [置顶] Maven搭建Mybatis-spring-springMVC项目记录
- java 搭建基于springboot的ssm(spring + springmvc + mybatis)的maven项目
- 二、maven项目搭建 springmvc+ spring + mybatis(2环境)
- idea 搭建 springMVC+mybatis+maven 项目(一)
- Maven+SpringMVC+Mybatis项目搭建(三)
- maven+spring+springmvc+mybatis之一个项目搭建的血泪史
- SpringMvc+MyBatis+MySQL+Maven+Redis搭建一个基础的web项目