springmvc注解事例

注解有简化 ,灵活性增强的功能

1.引入jar包

com.springsource.javax.validation-1.0.0.GA.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
hibernate-validator-4.1.0.Final.jar
log4j.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
spring-aop-3.2.2.jar
spring-aspects-3.2.2.jar
spring-beans-3.2.2.jar
spring-context-3.2.2.jar
spring-context-support-3.2.2.jar
spring-core-3.2.2.jar
spring-expression-3.2.2.jar
spring-tx-3.2.2.jar
spring-web-3.2.2.jar
spring-webmvc-3.2.2.jar

2.web.xml

springmvc注解事例
 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <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_3_0.xsd" id="WebApp_ID" version="3.0">

 3   <display-name>springmvcAnnotion</display-name>

 4   

 5   

 6   <servlet>

 7         <servlet-name>action</servlet-name>

 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

 9         <init-param>

10             <param-name>contextConfigLocation</param-name>

11             <param-value>classpath:spring-mvc.xml</param-value>

12         </init-param>

13         <load-on-startup>1</load-on-startup>

14     </servlet>

15     

16     <servlet-mapping>

17         <servlet-name>action</servlet-name>

18         <url-pattern>*.action</url-pattern>

19     </servlet-mapping>

20   

21   

22   

23   

24   <welcome-file-list>

25     <welcome-file>index.htm</welcome-file>

26     <welcome-file>index.jsp</welcome-file>

27   </welcome-file-list>

28 </web-app>
View Code

3.spring-mvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <beans xmlns="http://www.springframework.org/schema/beans"

 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 4     xmlns:mvc="http://www.springframework.org/schema/mvc"

 5     xmlns:context="http://www.springframework.org/schema/context"

 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 

 7             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 

 8             http://www.springframework.org/schema/mvc 

 9             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 

10             http://www.springframework.org/schema/context 

11             http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

12 

13         <!-- 自动扫描注解 -->

14     <context:component-scan base-package="cn.itcast.springmvc.web.controller"/>

15     

16     <!-- 注解驱动 -->

17     <mvc:annotation-driven/>

18     

19     

20     <!-- 内部资源视图解析器  prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp-->

21 <!--     用于解析在controller 中return  ModelAndView中的视图名的拼凑 -->

22     <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

23         <!-- 前缀 -->

24         <property name="prefix" value="/WEB-INF/jsps/"/>

25         <!-- 后缀 -->

26         <property name="suffix" value=".jsp"/>

27     </bean>

28     

29 </beans>

3.controller.java

 1 package cn.itcast.springmvc.web.controller;

 2 

 3 import javax.servlet.http.HttpServletRequest;

 4 import javax.servlet.http.HttpServletResponse;

 5 

 6 import org.springframework.stereotype.Controller;

 7 import org.springframework.web.bind.annotation.RequestMapping;

 8 import org.springframework.web.servlet.ModelAndView;

 9 import org.springframework.web.servlet.mvc.AbstractController;

10 //这里声明是注解

11 @Controller

12 public class HomeController extends AbstractController {

13     //这里声明访问路径

14     @RequestMapping(value={"/home.action","/aa.action"})

15     @Override

16     protected ModelAndView handleRequestInternal(HttpServletRequest request,

17             HttpServletResponse response) throws Exception {

18 

19 

20         System.out.println("hello" + request.getContextPath() + request.getServletPath());

21         

22         return new ModelAndView("index");

23     }

24 

25 }

这样就可以 通过localhost:8080/路径/aa.action  或者后面是home.action进行访问了

你可能感兴趣的:(springMVC)