首页 > 代码库 > 学习笔记---springMVC搭建

学习笔记---springMVC搭建

最近工作不是很忙,写项目总结总结学习和工作中的技术,标记...

我的环境配置:

操作系统win 7
开发工具  STS
web服务器tomcat7
javajdk1.6

1. 项目的创建,我是参考下面这篇文章

  http://fruzenshtein.com/setup-of-dynamic-web-project-using-maven/

2. 相关jar包的引入

  pom.xml中spring的配置

 1     <properties> 2         <org.springframework-version>3.1.1.RELEASE</org.springframework-version> 3  4     <dependencies> 5         <!-- Spring --> 6         <dependency> 7             <groupId>org.springframework</groupId> 8             <artifactId>spring-context</artifactId> 9             <version>${org.springframework-version}</version>10             <exclusions>11                 <!-- Exclude Commons Logging in favor of SLF4j -->12                 <exclusion>13                     <groupId>commons-logging</groupId>14                     <artifactId>commons-logging</artifactId>15                 </exclusion>16             </exclusions>17         </dependency>18         <dependency>19             <groupId>org.springframework</groupId>20             <artifactId>spring-webmvc</artifactId>21             <version>${org.springframework-version}</version>22         </dependency>23         <dependency>24             <groupId>org.springframework</groupId>25             <artifactId>spring-tx</artifactId>26             <version>${org.springframework-version}</version>27         </dependency>28         <dependency>29             <groupId>org.springframework</groupId>30             <artifactId>spring-jdbc</artifactId>31             <version>${org.springframework-version}</version>32         </dependency>33         <dependency>34             <groupId>org.springframework</groupId>35             <artifactId>spring-orm</artifactId>36             <version>${org.springframework-version}</version>37         </dependency>
View Code
 
3. spring相关配置:

  3.1 web.xml中的配置

 1      <!-- Processes application requests --> 2     <servlet> 3         <servlet-name>dispatcherServlet</servlet-name> 4         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5         <init-param> 6             <param-name>contextConfigLocation</param-name> 7             <param-value>classpath:spring/servlet-context.xml</param-value> 8         </init-param> 9         <load-on-startup>1</load-on-startup>10     </servlet>11 12     <servlet-mapping>13         <servlet-name>dispatcherServlet</servlet-name>14         <url-pattern>*.html</url-pattern>15     </servlet-mapping> 
View Code

  3.2 servlet-context.xml配置:文件的目录位置:/src/main/resources/spring/,配置关于控制层的转发

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 5     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 6         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 9     10     <!-- 搜索的控制类路径(C) -->11     <context:component-scan base-package="com.geek.*.controller" use-default-filters="false">12         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />13     </context:component-scan>14 15     <!-- 配置视图路径(V) -->16     <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->17     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">18         <property name="prefix" value="/WEB-INF/views/" />19         <property name="suffix" value=".jsp" />20     </bean>    21     22 </beans>
View Code

这样的话呢,基本的配置就算完成了,现在可以做个测试了.

新建controller,需要满足在servlet-context中配置的控制类路径:src/main/java/com/geek/home/controller/

package com.geek.home.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/**  * @ClassName: HomeController  * @date 2014-8-11 上午10:37:24   */ @Controllerpublic class HomeController {        @RequestMapping(value="/index.html", method = RequestMethod.GET)    public String home(){        return "home";    }}
View Code

新建jsp,需要满足在servlet-context中配置的路径:WEB-INF/views/

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 6 <title>Insert title here</title> 7 </head> 8 <body> 9 10 <p>This is the home page.</p>11 12 </body>13 </html>
View Code

访问路径:http://localhost:8080/ginkgo/index.html.

一切ok,O(∩_∩)O~~

 

参考文章:

  http://blog.csdn.net/linxcool/article/details/7094460

  http://www.open-open.com/lib/view/open1338338587698.html