基于Spring MVC的简单HelloWorld实例

1.导包

基于Spring MVC的简单HelloWorld实例

2.web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 	version="2.5">
     <servlet>  
         <servlet-name>dispatcherServlet</servlet-name>  
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
         <load-on-startup>1</load-on-startup>  
     </servlet>  
     <servlet-mapping>  
         <servlet-name>dispatcherServlet</servlet-name>  
         <url-pattern>/</url-pattern>  
     </servlet-mapping>  
 </web-app>

3.包结构定义以及控制器的编写


package com.techbirds.controller;
 
 import java.util.Map;
 
 @Controller
 public class HelloContorller {		
 	//hello world例子
 	@RequestMapping(value="/hello")
 	public String hello(){
 		System.out.println("spring mvc hello world!");
 		return "hello";
 	}
 }
 

4.xxxx-servlet文件配置

<?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:mvc="http://www.springframework.org/schema/mvc"
 	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
 		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
 	<!-- 自动扫描的包名 -->
 	<context:component-scan base-package="com.techbirds.controller"></context:component-scan>
 	<!-- 默认的注解映射的支持 -->
 	<mvc:annotation-driven />
 	<!-- 视图解释类 -->
 	<bean
 		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 		<property name="prefix" value="/WEB-INF/jsp/" />
 		<!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
 		<property name="suffix" value=".jsp" />
 	</bean>
 
 
 </beans>

5.返回的视图(jsp)编写

基于Spring MVC的简单HelloWorld实例

6.源码


你可能感兴趣的:(spring,mvc)