基于注解Spring MVC 3.0简单示例

jopen 10年前

首先说明一下使用的版本:

JDK:1.6   Tomcat:6.0   Spring:3.2,使用到的Jar包截图:

基于注解Spring MVC 3.0简单示例

工程结构截图:

基于注解Spring MVC 3.0简单示例

接下来: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">      <servlet>    <servlet-name>SpringMVC</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>     <param-name>contextConfigLocation</param-name>     <param-value>classpath:SpringMVC-servlet.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup>   </servlet>   <servlet-mapping>    <servlet-name>SpringMVC</servlet-name>    <url-pattern>/</url-pattern>   </servlet-mapping>      <!-- 编码过滤 -->        <filter>            <filter-name>encode</filter-name>            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>            <init-param>                <param-name>encoding</param-name>                <param-value>UTF-8</param-value>            </init-param>        </filter>        <filter-mapping>            <filter-name>encode</filter-name>            <url-pattern>/*</url-pattern>        </filter-mapping>   <welcome-file-list>    <welcome-file>WEB-INF/index.jsp</welcome-file>   </welcome-file-list>  </web-app>

log4j.properties文件配置:

log4j.rootLogger=WARN,stdout    log4j.appender.stdout=org.apache.log4j.ConsoleAppender    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout    log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH\:mm\:ss}%n[%p]-[Thread\: %t]-[%C.%M()]\: %m%n

SpringMVC-servlet.xml文件的配置:

<?xml version="1.0" encoding="UTF-8"?>  <beans default-lazy-init="true"         xmlns="http://www.springframework.org/schema/beans"           xmlns:p="http://www.springframework.org/schema/p"         xmlns:tx=""                xmlns:aop="http://www.springframework.org/schema/aop"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns:context=         xmlns:mvc=         xsi:schemaLocation=   "                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">         <!-- 注解驱动 -->      <mvc:annotation-driven />      <!-- 扫描包 -->      <context:component-scan base-package="com.project.controller" />   <!-- ViewResolver -->   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>       <property name="prefix" value="/WEB-INF/jsp/"/>       <property name="suffix" value=".jsp"/>   </bean>   <!-- HandlerAdapter -->   <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    <property name="mappings">     <props>      <prop key="/index">Hello</prop>     </props>    </property>   </bean> -->   <!-- HandlerMapping -->   <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>   <!-- HandlerMapping -->   <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>   <!-- <bean name="/index" class="com.project.controller.HelloController"/> -->   <!-- <bean id="Hello" class="com.project.controller.HelloController" /> -->  </beans>

HelloController代码内容:

package com.project.controller;  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletResponse;  import org.springframework.stereotype.Controller;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.servlet.ModelAndView;  @Controller  @RequestMapping("/hello")  public class HelloController {   @RequestMapping("/index")   public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {    ModelAndView modelView = new ModelAndView();    modelView.addObject("msg","Hello Index");    modelView.setViewName("index");    System.err.println("执行index!");    return modelView;   }   @RequestMapping("/show")   public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {    ModelAndView modelView = new ModelAndView();    modelView.addObject("msg","Hello show");    modelView.setViewName("index");    System.err.println("执行show!");    return modelView;   }  }

OK,运行效果如图:

基于注解Spring MVC 3.0简单示例