我的日常

登录/注册
您现在的位置:论坛 盖世程序员(我猜到了开头 却没有猜到结局) 盖世程序员 > Spring MVC 3.x注解方式实例源码
总共48087条微博

动态微博

查看: 1260|回复: 0

Spring MVC 3.x注解方式实例源码

[复制链接]
admin    

1244

主题

544

听众

1万

金钱

管理员

  • TA的每日心情

    2021-2-2 11:21
  • 签到天数: 36 天

    [LV.5]常住居民I

    管理员

    跳转到指定楼层
    楼主
    发表于 2015-06-01 20:20:51 |只看该作者 |倒序浏览

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

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

    工程结构截图:

    接下来:web.xml文件配置:

    1. <?xml version="1.0" encoding="UTF-8"?>& f& a5 ]3 ~/ h) J7 d7 g# G
    2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
      $ o! U- \. s4 G- B6 f
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"" G: o. D1 Z1 t6 [# K* t
    4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      ; T9 a/ C% U# x0 t) e5 S2 d2 B
    5. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">4 P) j. e' }$ L

    6. , W) {, M; R6 Q( u8 U) z
    7. <servlet>! L6 d, T' W( D) E2 |: j; V
    8.   <servlet-name>SpringMVC</servlet-name>) ]! M7 D, E0 c# R2 a
    9.   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      ! F7 Z9 r0 h( s3 M# E
    10.   <init-param>
      " B) m/ Q- `: Q$ G$ a: s
    11.    <param-name>contextConfigLocation</param-name>
      8 ^5 J3 ~2 A5 ^/ M3 p3 k2 [6 u
    12.    <param-value>classpath:SpringMVC-servlet.xml</param-value>3 S2 Y/ c5 F. h% R
    13.   </init-param>
      2 q% `4 o# P' }: p1 _) }% r
    14.   <load-on-startup>1</load-on-startup>3 _' `- t; E7 @# z1 n( V' r
    15. </servlet>
      ; R( r! B& \% Z' A+ ^2 V! y) v
    16. <servlet-mapping>$ G3 {6 n+ L  X" W; U+ K. T
    17.   <servlet-name>SpringMVC</servlet-name>1 n" |! D: P  T0 A4 v
    18.   <url-pattern>/</url-pattern>
      & W  \7 y- l+ z5 e% z
    19. </servlet-mapping>
      2 m3 W: r: B0 y  U6 B; w/ Z

    20. 9 Z% T- @- [4 ^! l1 U5 y( H! c3 V
    21. <!-- 编码过滤 -->  
      7 T% `# |8 ~" t, ], f% w
    22.     <filter>  
      4 N+ t5 |! K3 t: T4 a
    23.         <filter-name>encode</filter-name>  
      , d( ^. H4 [) D- z1 H
    24.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
      ) C7 B4 V; w2 ?$ c+ Y
    25.         <init-param>  
      5 d+ x0 n4 n% ~, ^; k6 e7 Z
    26.             <param-name>encoding</param-name>  
      ! m/ N( o  [9 c0 v" |4 |
    27.             <param-value>UTF-8</param-value>  
      3 E7 u% ^' l. ?1 D
    28.         </init-param>  
      3 x4 L# Z; n. L
    29.     </filter>  
      ' X) G# ^9 _! B# X& ~* T
    30.     <filter-mapping>  & k( b  ?3 u* }$ k0 A) O" X
    31.         <filter-name>encode</filter-name>  
      , E, ]3 W- x, n$ t* `0 O
    32.         <url-pattern>/*</url-pattern>  
      4 E$ ~# ]; i0 s! u  v) q
    33.     </filter-mapping>
      3 l) g4 z4 w( I# e! ^' ~
    34. <welcome-file-list>
      # D1 Z2 D/ i& Q% f2 X
    35.   <welcome-file>WEB-INF/index.jsp</welcome-file>) u& G' S! Q  d8 U/ J
    36. </welcome-file-list>
      3 g- O$ C3 Y; @1 V
    37. </web-app>
    复制代码

    7 x4 a) ~8 E3 a" B( U
    log4j.properties文件配置:

    1. log4j.rootLogger=WARN,stdout
      7 l- ]6 V& ~/ t1 d% T6 h
    2. ( E% A5 r; O: L! ^+ e$ q
    3. log4j.appender.stdout=org.apache.log4j.ConsoleAppender: g) u4 |9 f- S$ s& r# F
    4. 3 p) x* C6 {3 L  R
    5. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
      2 ^0 z  H- J8 H# Y: [. D

    6. 6 G+ `3 G2 t- z. K: p7 q6 D7 k
    7. log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
    复制代码
    , Z9 A) F$ D5 }( Q
    SpringMVC-servlet.xml文件的配置:

    1. <?xml version="1.0" encoding="UTF-8"?>
      & P# L8 d  }$ O0 t1 P: B) b* w
    2. <beans default-lazy-init="true"' C% D, m7 B9 F5 K4 x$ |' V
    3.        xmlns="http://www.springframework.org/schema/beans"  
      ) z; s: d* E- H# i" ?% Y
    4.        xmlns:p="http://www.springframework.org/schema/p". l+ t3 q4 U3 N" _, G
    5.        xmlns:tx=""       ' T) D; w0 Z( ]/ E/ f6 z! ^2 B
    6.        xmlns:aop="http://www.springframework.org/schema/aop"
      # Q! D8 r0 t/ h, K0 B6 `3 L( ]
    7.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  ( D% C$ \2 q" w/ b; N
    8.        xmlns:context=
      - U% f, }$ m5 K
    9.        xmlns:mvc=2 G! z: d+ L5 t1 b
    10.        xsi:schemaLocation= % P# i6 ]+ z$ T: i, Q  {
    11. "                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
      7 b8 \3 B8 o( P* ]5 h1 [5 p0 u# _1 l9 {

    12. 5 n; B- k  z' j4 p  l! Z' s1 c" m
    13.     <!-- 注解驱动 -->
      ' X& b0 t* h& U6 ]
    14.     <mvc:annotation-driven />
      . y8 T# x% W6 E: p; }; V$ I
    15.     <!-- 扫描包 -->
      " z8 j$ ^$ Q. S' y/ b1 c5 j# g
    16.     <context:component-scan base-package="com.project.controller" />
      5 q, Y3 f0 t) N7 G7 s; N- @: C4 [
    17. <!-- ViewResolver -->$ Y* F# P, K4 ~# w" S# r( `
    18. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      ) C  E9 M! r3 n1 j1 }  ^
    19.      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>6 ]+ r2 u% b& o# a
    20.      <property name="prefix" value="/WEB-INF/jsp/"/>" I, B- d+ v! [/ e0 ~- Z- R- a# x
    21.      <property name="suffix" value=".jsp"/>" x( O9 \! i# w7 l+ C
    22. </bean>* H6 h; @0 M: ~& L# V) \0 }, \
    23. <!-- HandlerAdapter -->
      9 a) H  @" y. l1 B. v
    24. <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      " m7 z5 d5 s  j$ a4 k9 _$ V
    25.   <property name="mappings">* {# g# f8 \% K
    26.    <props>
      0 {  q1 [% [" k! U5 {) m7 A1 k4 c
    27.     <prop key="/index">Hello</prop>, X( Y8 L3 O# @! p% \
    28.    </props>
      ) F) @4 n8 t' o0 e8 y4 O" W
    29.   </property>
      - e6 R6 a# J; @+ z) Y
    30. </bean> -->! Q) Q  O  f, \' T
    31. <!-- HandlerMapping -->2 m7 l/ Y! E# {2 S$ s
    32. <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>5 g3 z$ o" I% T5 ]; Q& O8 C- V
    33. <!-- HandlerMapping -->
      7 Y2 @/ n* x9 t$ t0 V! P" m4 U) p
    34. <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>5 G! I9 T% }* z2 c2 ~" U& D7 L' Z. y
    35. <!-- <bean name="/index" class="com.project.controller.HelloController"/> -->
      , R4 ]% D4 S, I) U* I% t
    36. <!-- <bean id="Hello" class="com.project.controller.HelloController" /> -->1 W- K! q" m! ~% G5 m* ~
    37. </beans>
    复制代码
    + i2 ~1 Y- Z. A& K7 z2 T
    HelloController代码内容:

    1. package com.project.controller;
      $ Y6 |4 L1 m0 g# n6 j4 [
    2. import javax.servlet.http.HttpServletRequest;3 v+ i8 n0 x, _! j" d* B
    3. import javax.servlet.http.HttpServletResponse;' \  e: a: ~/ W3 [' f
    4. import org.springframework.stereotype.Controller;
      + @; E! [$ F8 Y9 k! i( g0 A
    5. import org.springframework.web.bind.annotation.RequestMapping;$ j$ T/ e" N& O6 @" O( L
    6. import org.springframework.web.servlet.ModelAndView;
      ! A, _( ]1 @9 `# k9 a% K) h
    7. @Controller
      ; v% T% p9 @; \$ m# q) n
    8. @RequestMapping("/hello")! N+ C4 M5 J0 w8 E1 I- s
    9. public class HelloController {
      7 c& O+ E! v. g
    10. @RequestMapping("/index")
        G. l% @' X8 U3 B
    11. public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
      1 v2 t4 p% h" C4 j! e
    12.   ModelAndView modelView = new ModelAndView();9 S! n: m; h9 Q: |) n8 e* y
    13.   modelView.addObject("msg","Hello Index");: C+ M/ Y) ?+ C2 I' V( h5 M* `
    14.   modelView.setViewName("index");
      8 b* W2 ]* F1 X) v
    15.   System.err.println("执行index!");- T' _# k0 _! [. H% t- L9 M* r
    16.   return modelView;+ z$ u, h0 Q# \4 v
    17. }+ P  H2 U4 W% ~8 H" V& F
    18. @RequestMapping("/show")# `. d$ y) f2 L6 e) ~1 ?
    19. public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
      - ]3 ?4 |. B" Q: Q
    20.   ModelAndView modelView = new ModelAndView();
      * G8 ?4 r$ Z5 T
    21.   modelView.addObject("msg","Hello show");
      8 m& q& a) M9 D# f0 j; u# a5 P0 v- c
    22.   modelView.setViewName("index");6 G9 o8 _6 I. [, q' ~! y
    23.   System.err.println("执行show!");+ w# I4 A* q6 L9 I* U* M
    24.   return modelView;
      " y9 J+ {  C' Q
    25. }
      $ C* `3 ]; A5 U& L
    26. }
    复制代码
    ( t- y2 B/ B/ |/ |
    OK,运行效果如图:

    % o4 N* [/ I& r; D4 n* i* _, W: _/ I, i


    & G; R3 e* X2 J" r# w

    科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与科帮网享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、科帮网管理员和版主有权不事先通知发贴者而删除本文


    JAVA爱好者①群:JAVA爱好者① JAVA爱好者②群:JAVA爱好者② JAVA爱好者③ : JAVA爱好者③

    快速回复
    您需要登录后才可以回帖 登录 | 立即注册

       

    关闭

    站长推荐上一条 /1 下一条

    发布主题 快速回复 返回列表 联系我们 官方QQ群 科帮网手机客户端
    快速回复 返回顶部 返回列表