Spring MVC注解配置简单示例

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">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/classes/spring/*-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

applicationContext.xml暂时不需要添加什么

 

springmvc-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/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-autowire="byName">

	<!-- 自动搜索@Controller标注的类 --> 
	<context:component-scan base-package="com.gary.test.controller" />

	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
			
	<!-- InternalResourceViewResolver在UrlBasedViewResolver的基础上,添加了jstl的支持,相当于包含viewClass=jstlView -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
	</bean>	
        
</beans>

 

TestController.java

package com.gary.test.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
//设置controller的路径为/test
@RequestMapping("/test")
public class TestController {
	
	
	@RequestMapping("/start")
	public String start(){
		System.out.println("进入到TestController的start方法");
		return "test";
	}
	
//	@RequestMapping("/testParam")
//	public String testParam(HttpServletRequest request){
//	}
	
	///test/testParam.do?key=value
	//restful风格test/restParam/value.do
	/*
	 * 
	 * 如果路径中有点".",而点是特殊字符,比如.html, .do等等,所以Spring MVC默认是把点后面的信息当作文件后缀,需要修改这个默认值
	 * <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
	 *   <property name="useDefaultSuffixPattern" value="false" />
	 * </bean> 
	 * 然后设置@RequestMapping("/news/{title:.*}")
	 */
	@RequestMapping("/testParam/{strVal}/{intVal}/{dateVal}")
	public ModelAndView testParam(@PathVariable("strVal") String strVal, 
			@PathVariable("intVal") int intVal,@PathVariable("dateVal") Date dateVal){
		System.out.println("strVal=" + strVal + ",intVal=" + intVal + ",dateVal=" + dateVal);
		ModelAndView mav = new ModelAndView("testParam");
		mav.addObject("strVal", strVal);
		mav.addObject("intVal", intVal);
		mav.addObject("dateVal", dateVal);
		return mav;
	}
	
	/**
	 * 绑定日期类型数据
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}
	
	@RequestMapping(value="/testMethod", method = RequestMethod.GET)
	public String testGetMethod(){
		System.out.println("get method");
		return "get";
	}
}

 

测试连接:

<a href="<%=basePath %>test/start.do">test/start.do</a><br/>
<a href="<%=basePath %>test/testParam/str/1/2011-06-15.do">test/testParam/str/1/2011-06-15.do</a><br/>
<a href="<%=basePath %>test/testMethod.do">test/testMethod.do</a><br/>

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