如何搭建springMVC开发环境
展开全部
由于本人基础比较差,每隔一段时间就会忘记如何搭建spring,故写下来,一加深记忆、二方便下次快速浏览。下面是基于最简单的SpringMVC搭建。
搭建SpringMVC的基本步骤(基于Maven项目):
1、在pom.xml中添加spring-webmvc
2、配置web.xml
3、配置应用上下文spring-servlet.xml
---------------------------------------------------------------------------
一、在pom.xml中为项目添加SpringMVC必须的jar包:
[html] view plain copy
<properties>
<spring.version>4.0.2.RELEASE</spring.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- end -->
<!-- httl -->
<dependency>
<groupId>com.github.httl</groupId>
<artifactId>httl-springmvc</artifactId>
<version>1.0.11</version>
</dependency>
<!-- end -->
代码中httl的包是用于代替JSP作为视图输出数据给客户端。
二、配置web.xml
[html] view plain copy
<!-- 1.加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/maven20150807-servlet.xml
classpath*:/maven20150807-security.xml
</param-value>
</context-param>
<!-- 2.配置Spring监听 -->
<!-- 上下文加载监听器:作用就是启动Web容器时,自动装配ApplicationContext的配置信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 3.配置spring核心servlet -->
<servlet>
<servlet-name>maven20150807</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>maven20150807</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
三、配置spring-servlet.xml
[html] view plain copy
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.hason.mvc.controller"></context:component-scan>
<!--对静态资源文件的访问,location是物理路径-->
<mvc:resources location="/WEB-INF/images/*" mapping="/images/**"></mvc:resources>
<!-- HTTL 定义视图分解器,对模型视图名称的解析 -->
<span style="white-space:pre"> </span><bean id="viewResolver" class="httl.web.springmvc.HttlViewResolver">
<property name="contentType" value="text/html; charset=UTF-8" />
<property name="prefix" value="/WEB-INF/HTTL-Templates/" />
<property name="suffix" value=".httl" />
</bean>
搭建SpringMVC的基本步骤(基于Maven项目):
1、在pom.xml中添加spring-webmvc
2、配置web.xml
3、配置应用上下文spring-servlet.xml
---------------------------------------------------------------------------
一、在pom.xml中为项目添加SpringMVC必须的jar包:
[html] view plain copy
<properties>
<spring.version>4.0.2.RELEASE</spring.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- end -->
<!-- httl -->
<dependency>
<groupId>com.github.httl</groupId>
<artifactId>httl-springmvc</artifactId>
<version>1.0.11</version>
</dependency>
<!-- end -->
代码中httl的包是用于代替JSP作为视图输出数据给客户端。
二、配置web.xml
[html] view plain copy
<!-- 1.加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/maven20150807-servlet.xml
classpath*:/maven20150807-security.xml
</param-value>
</context-param>
<!-- 2.配置Spring监听 -->
<!-- 上下文加载监听器:作用就是启动Web容器时,自动装配ApplicationContext的配置信息 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 3.配置spring核心servlet -->
<servlet>
<servlet-name>maven20150807</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>maven20150807</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
三、配置spring-servlet.xml
[html] view plain copy
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.hason.mvc.controller"></context:component-scan>
<!--对静态资源文件的访问,location是物理路径-->
<mvc:resources location="/WEB-INF/images/*" mapping="/images/**"></mvc:resources>
<!-- HTTL 定义视图分解器,对模型视图名称的解析 -->
<span style="white-space:pre"> </span><bean id="viewResolver" class="httl.web.springmvc.HttlViewResolver">
<property name="contentType" value="text/html; charset=UTF-8" />
<property name="prefix" value="/WEB-INF/HTTL-Templates/" />
<property name="suffix" value=".httl" />
</bean>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询