• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jiajia920716
博客园    首页    新随笔    联系   管理    订阅  订阅
Spring MVC 初使用

  最近刚刚开始接触Spring MVC,由于项目有需要用到,所以特意去学习了一下。

  之前一直使用的是SSH框架,感觉Spring MVC比之前使用的框架更加好用,主要是Spring MVC提供了注解机制,省去了一堆麻烦的配置工作。

  这里我们使用Spring MVC实现一个最简单的web应用,主要是基于Spring MVC的注解机制。

  下面我们来看实现代码:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
    <display-name></display-name>    
    <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
  
    <servlet>
      <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        -->
    <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
     
    <!-- Spring配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
      
    <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:com/config/applicationContext.xml</param-value>
    </context-param>
    -->
</web-app>

  运行程序过程中,首先读取web.xml中的内容,在这里,我们主要配置了以下几项:

  1. welcome-file-list: 指定程序运行时显示的第一个页面;

  2. servlet: 使用DispatcherServlet(前置控制器),负责拦截匹配的请求,然后依据一定的规则分配给相应的Controller进行处理;

  3. servlet-mapping: 确定拦截的url模式,这里我们要拦截的是以“.do”结尾的url;

  4. listener: 这里主要用ContextLoaderListener,它能自动加载applicationContext.xml文件。

 

spring-servlet.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"     
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 启用spring mvc 注解-->
    <context:annotation-config /> 

    <!-- 设置使用注解的类所在的jar包 -->
    <context:component-scan base-package="com.mvc" />
          
    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <!-- ViewResolver -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>  
        <property name="prefix" value="/"/>  
        <property name="suffix" value=".jsp"/>  
    </bean>    
</beans>

  系统会根据web.xml中的配置自动找到servlet的配置文件进行加载。

  启用spring mvc注解之后,我们就能在源文件中使用注解,系统会自动在com.mvc包里面寻找含有注解的代码

  而ViewResolver是一个url路径的解析器,当控制器返回一个字符串时,系统会自动给这个字符串加上前缀和后缀,生成一个有效的url。比如控制器返回“success”,最终生成 的url就为:http://localhost:8080/[ApplicationName]/success.jsp 。

  

applicationContext.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
        
        
</beans>

  在这个文件中,我基本上没有写任何有用的内容。

 

HelloController.java

package com.mvc.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.mvc.service.HelloService;

@Controller
public class HelloController {
    @Autowired
    private HelloService helloService;

    @RequestMapping("/hello.do")
    public ModelAndView hello() {
        ModelAndView mv = new ModelAndView("success");
        mv.addObject("message", helloService.hello());
        return mv;
    }
}

  @Autowired: 系统会自动寻找HelloService进行注入,不用写getter,setter方法就能使用。

  @RequestMapping: 映射规则。控制器拦截到hello.do结尾的url就会分发给hello方法去处理。

  

HelloService.java

package com.mvc.service;

public interface HelloService {
    public String hello();
}

HelloServiceImpl.java

package com.mvc.service;

import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello() {
        return "hello world";
    }

}

在上面的@Autowired寻找HelloService,会依据@Service标签找到这个类进行注入。

 

至此,我们就完成了一个最简单的Spring MVC应用。

 

源代码

 

  

  

 

posted on 2014-03-20 22:41  jiajia920716  阅读(249)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3