码迷,mamicode.com
首页 > 编程语言 > 详细

spring mvc构建WEB应用程序入门例子

时间:2014-05-02 11:57:36      阅读:460      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

在使用spring mvc 构建web应用程序之前,需要了解spring mvc 的请求过程是怎样的,然后记录下如何搭建一个超简单的spring mvc例子。

1) spring mvc的请求经历

 

请求由DispatcherServlet分配给控制器(根据处理器映射),在控制器完成处理后,请求会被发送到一个视图(根据viewController解析逻辑视图) 来呈现输出结果。

整理成下图所示:

bubuko.com,布布扣

 

2)搭建一个简单的spring mvc例子

①创建一个maven工程,其中pom中要有spring相关的坐标:

bubuko.com,布布扣
bubuko.com,布布扣
 <properties>
      <spring.version>3.2.1.RELEASE</spring.version>
  </properties>
  <dependencies>
          <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>

          <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
        <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity</artifactId>
                <version>1.7</version>
            </dependency>
            <dependency>
                <groupId>org.apache.velocity</groupId>
                <artifactId>velocity-tools</artifactId>
                <version>2.0</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.struts</groupId>
                        <artifactId>struts-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.apache.struts</groupId>
                        <artifactId>struts-taglib</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.apache.struts</groupId>
                        <artifactId>struts-tiles</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>sslext</groupId>
                        <artifactId>sslext</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>servletapi</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.4</version>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>jspapi</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.0</version>
                <optional>true</optional>
            </dependency>
  </dependencies>
View Code
bubuko.com,布布扣

 

②web.xml中需要配置DispatcherServlet,它是spring mvc前端控制器,请求时是通过它处理的:

 

bubuko.com,布布扣
<servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
bubuko.com,布布扣

 

 DispatcherServlet是根据一个xml文件来加载其spring上下文的,文件名基于<servlet-name>,因为声明的servlet-name为dispatcher,因此需要有一个dispatcher-servlet.xml。

 考虑这种情况:若存在多个xml文件,比如xxx-dao.xml,xx-service.xml这种分层的文件,如何将这些文件一同加载到spring的上下文中呢?这就要通过ContextLoaderListener,它是一个servlet监听器,除了DispatcherServlet创建的上下文外,它能将将其他文件加载到一个spring上下文中。需要在web.xml配置:

bubuko.com,布布扣
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
bubuko.com,布布扣

需要告诉ContextLoaderListener需要加载哪些配置文件,若没有指定的话,上下文加载器会查找/WEB-INF/applicationContext.xml配置文件;若正好声明的spring配置文件名字不是applicationContext.xml,运行的时候是会抛出一个ClassNotFound这个异常的。

所以,配置的web.xml为:

bubuko.com,布布扣
bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
  <display-name>Archetype Created Web Application</display-name>
  
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-config.xml</param-value>
    </context-param>
    
    <context-param>    
        <param-name>log4jConfigLocation</param-name>    
        <param-value>classpath:log4j.xml</param-value>    
    </context-param> 
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <welcome-file-list>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/error/404.html</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/error/404.html</location>
    </error-page>
    <error-page>
        <error-code>403</error-code>
        <location>/error/404.html</location>
    </error-page>
</web-app>
View Code
bubuko.com,布布扣

 

③ 配置文件spring-config.xml和dispatcher-servlet.xml

spring-config.xml:

bubuko.com,布布扣
<!-- 自动检测Bean -->
    <context:component-scan base-package="com.test.service.impl" />
    
    <!-- 自动装配Bean -->
    <context:annotation-config />
bubuko.com,布布扣

 

dispatcher-servlet.xml:

bubuko.com,布布扣
<!-- 处理静态资源 -->
<!--     <mvc:resources location="resources/*" mapping="resources/**"/> -->
    
    
    <!-- 启动spring mvc注解 -->
    <mvc:annotation-driven/>
     
    <!-- 自动检测Controller类 -->

    <context:component-scan base-package="com.test.controller"/>
    <!-- velocity 配置 -->
    <bean id="velocityConfig"
        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/vm/" />
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
                <prop key="velocimacro.library">macro/macros.vm</prop>
            </props>
        </property>
    </bean>
    
    
    <!-- 配置视图解析器,用于解析逻辑视图名-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
        <property name="cache" value="false" />
        <property name="prefix" value="" />
        <property name="suffix" value=".vm" />
        <property name="layoutUrl" value="layout/layout.vm" />
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="exposeSpringMacroHelpers" value="true"/>
        <property name="dateToolAttribute" value="dateTool" />
    </bean>

 

bubuko.com,布布扣

 

 ④ Controller类:

  

bubuko.com,布布扣
@Controller
public class HomeController {
    
    private UserService userService;
    
    @Autowired
    public HomeController(UserService userService){
        this.userService = userService;
    }
    
    //指定请求的路径
    @RequestMapping("home.html")
    public String showHomePage(Map<String,Object> model){
        model.put("count", userService.getUsersCount());
        return "home";
    }
}
bubuko.com,布布扣

 

showHomePage对应的请求为:localhost/项目名/home.html

 

 整个项目工程图:

 bubuko.com,布布扣

 

 

spring mvc构建WEB应用程序入门例子,布布扣,bubuko.com

spring mvc构建WEB应用程序入门例子

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/wmmang-blog/p/3703465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!