Loading

SpringMVC Hello World

前言

       新年伊始,元宵佳节,窗外灯火通明,炮声连连。北漂以来第一次一个人在北京过十五。

       切入正题,收假后一边要赶项目进度还要学习java,so在元宵佳节之际写了第一篇SpringMVC Hello World。

       

 

开发环境

      MyEclipse 10、Tomcat 7、jdk1.7

 

项目结构及Spring依赖jar包

    刚开始学Spring,还没用maven。

 

配置文件

    开发Spring,配置文件自然少不了,web.xml、spring xml、spring mvc xml。

 

web.xml

   配置spring mvc前端控制器DispatcherServlet及拦截request请求url-pattern

<!--监听spring 上下文 -->
  <listener>
  	<listener-class>
  		org.springframework.web.context.ContextLoaderListener
  	</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>/WEB-INF/root-context.xml</param-value>
  </context-param>
  
  <!-- 配置Spring MVC DispatcherServlet -->
  <servlet>
  	<servlet-name>MVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<!-- 加载spring mvc的xml到Spring上下文中 -->
  		<param-name>contextConfigLocation</param-name>
  		<param-value>/WEB-INF/mvc-context.xml</param-value>
  	</init-param>
  </servlet>
  <!--配置Spring DispatcherServlet拦截器 -->
  <servlet-mapping>
  	<servlet-name>MVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>

  

spring mvc xml

<beans:beans xmlns="http://www.springframework.org/schema/mvc"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans"  
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    			http://www.springframework.org/schema/mvc/spring-mvc.xsd  
                http://www.springframework.org/schema/aop  
                http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
                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.xsd">  
    <!-- 加载Spring的全局配置文件 -->  
    <beans:import resource="root-context.xml" />  
      
    <!-- SpringMVC配置 -->        
    <context:component-scan base-package="com.autohome.controller" />  
    <mvc:annotation-driven /> 
    <!-- 配置SpringMVC的视图渲染器, 让其前缀为:/ 后缀为.jsp  将视图渲染到/page/<method返回值>.jsp中 -->  
    <beans:bean  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
        p:prefix="/views/" p:suffix=".jsp">  
    </beans:bean>  
  
  
</beans:beans>

  

root-context.xml

<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-3.2.xsd  
                http://www.springframework.org/schema/context  
                http://www.springframework.org/schema/context/spring-context-3.2.xsd  
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  
      
  
       
</beans>

  

编写第一个Controller

    采用注解的方式开发Controller主要使用属性Controller、RequestMapping

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;



@Controller
@RequestMapping("/Home")
public class HomeController {
	@RequestMapping("Index")
	public ModelAndView index(){
		ModelAndView view =new ModelAndView("index");
		return view;
	}
}

 方法中定义了ModelAndView对象,通过该对象指定所需要渲染的视图为index最后返回ModelAndView 将页面渲染到index.jsp中。 views视图文件夹我放在WEB-INF目录下,jsp没有什么内容,就不贴代码了,直接新建即可。

   OK,部署tomcat,第一个Spring MVC搞定

 

posted @ 2017-02-11 19:13  歪头儿在北京  阅读(606)  评论(5编辑  收藏  举报