-
http://www.blogbus.com/wanping-logs/235898637.html
使用注解的原因
最方便的還是啟用注解
注解方便,而且項目中很流行。
配置文件盡量減少,主要使用注解方式。
Springmvc的注解是在2.5版本后有了注解,如何開啟注解?
修改springmvc配置文件
Web.xml文件中不需要修改,只修改springmvc配置文件
新建一個springmvc的配置文件,取名為springAnnotation-servlet.xml
刪除掉之前文件中的bean和多方法的配置,springAnnotation-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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 靜態資源訪問 -->
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
配置分兩步
1)配置掃描包
這個配置文件的書寫需要一個掃描包。
在springAnnotation-servlet.xml中配置
<context:component-scan base-package="com.tgb.web.controller.annotation"></context:component-scan>
這個配置的意思是:
Spring再啟動的時候,會默認掃描自動掃描包下的所有的類,為每個注解分配一個mapping。
上面的配置是,在啟動的時候會掃描com.tgb.web.controller.annotation下所有的包
2)配置spring注解的兩個bean
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
這兩個bean的功能
1)AnnotationMethodHandlerAdapter
是方法映射的,不同方法有不同url請求,根基類找方法。
2)DefaultAnnotationHandlerMapping
DefaultAnnotationHandlerMapping根據掃描的包下面找類,即通過url找類
注意:
一定要把包找對,我就犯過錯,兩個包都在org.springframework.web.portlet目錄下找的類,我用的是兩個包分別是
org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping
其實應該是org.springframework.web.servlet目錄下找類。
兩個包分別是:
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
修改web.xml中的springmvc的配置文件
修改web.xml文件,使其使用新建的springmvc配置文件springAnnotation-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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 注解掃描包 -->
<context:component-scan base-package="com.tgb.web.controller.annotation" />
<!-- 開啟注解 -->
<!-- bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<!-- 靜態資源訪問 -->
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
新建一個類(userController)來實現注解
新建包
新建一個包,下面的controller都使用注解
新建controller的包,在這個包下的controller都使用配置文件。
選擇com.tgb.web.controller,郵件點擊new-other-package,填新建的包名為annotation。
新建類userController(GET請求)
注解打開后,看看注解如何使用。
userController類實現如下:
package com.tgb.web.controller.annotation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@RequestMapping(value="/user/addUser",method=RequestMethod.GET)
public ModelAndView adduser(){
String result = "this is adduser------";
return new ModelAndView("/annotation","result",result);
}
@RequestMapping(value="/user/delUser",method=RequestMethod.GET)
public ModelAndView delUser(){
String result = "this is delUser------";
return new ModelAndView("/annotation","result",result);
}
}
Spring常用注解:
<!--[if !supportLists]-->1. <!--[endif]-->類的注解
類注解用到了@Controller
@Controller表明下面是個Controller類
org.springframework.stereotype.Controller
@Component
@Target(value={TYPE})
@Retention(value=RUNTIME)
@Documented<!--[if !supportLists]-->2. <!--[endif]-->方法注解
方法注解用到了@RequestMapping
@RequestMapping表明下面的方法是個Controller的方法。
org.springframework.web.bind.annotation.RequestMapping
@Mapping
@Target(value={METHOD, TYPE})
@Retention(value=RUNTIME)
由上面的信息可以看出,注解類RequestMapping的返回值是Mapping,方法參數有2個,第一個是Method,第二個是Type,即Method是請求的方法,Type是請求的類似是get還是post。@RequestMapping(value="/user/delUser",method=RequestMethod.GET)
新建一個view——annotation.jsp
新建一個view來實現userController的效果
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.min.js"></script>