最简单的Spring mvc 的配置

 配置spring mvc 之前需要了解一下spring mvc的架构

一、分析:

鉴于上图

1. 需要配置FrontController前端控制器DispatchServlet

2. 需要配置view template也就是viewResolver

3. controller需要程序员自己是实现

4. 整个流程的管理由spring mvc的框架接管

二、配置

1. 在web.xml中声明所使用的前端控制器(可以通过sts的图形界面创建一个dispatchServlet搞定)

<servlet>
    <description></description>
    <display-name>shopping</display-name>
    <servlet-name>shopping</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>shopping</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

实际上配置的是spring mvc框架与servlet的接口,比如servlet接收的url的范围,对应的是图中incoming request的部分

 

2.在文件servlet_name.xml中进行配置(这个文件是约定的servlet的文件名称,同时框架默认的搜索路径是WebContent/WEB-INF/servletname-servlet.xml)

(1). 配置viewResolver(搜索的路径是)

bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

(2).配置context上下文的全局策略

<context:annotation-config></context:annotation-config>
    <context:component-scan base-package="controller"></context:component-scan>

配置component-scan的文件夹controller,作为上下文中的bean

三、实现controller

@Controller
public class HomeController {
	@RequestMapping("/")
	@ResponseBody
	public String home(){
		return "home";
	}
	
	@RequestMapping("/showhome")
	public String showHome(){
		return "showhome";
	}
}

补充配置:

1. 在web.xml中配置其他bean的配置文件

 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:config/model_context.xml
        </param-value>        
    </context-param>

2. 相关的model_context.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"
    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-4.1.xsd">

    
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="/model"></context:component-scan>
</beans>

在Controller中使用其他的bean---Writer 

@Component
public class Writer {

	public void output() {
		System.out.println("writer output");
	}
}

 

@Controller
public class HomeController {
	@Autowired 
	private Writer writer;
	
	@RequestMapping("/")
	@ResponseBody
	public String home(){
		writer.output();
		return "home";
	}
	
	@RequestMapping("/showhome")
	public String showHome(){
		return "showhome";
	}
}

 

 

项目创建的大致截图:

1. 使用sts创建一个dynamic web project 项目

2. convert to maven project 

依赖如下:

目录结构如下

 

posted @ 2015-07-09 14:27  Tony_DFS  阅读(256)  评论(0编辑  收藏  举报