一、使用註解減小配置xml文件java
二、註解能夠下降耦合度web
三、使用註解編寫的普通類spring
import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
//說明springMvc controller 類 @Controller public class testAction {
//設置請求路徑
//返回值須要使用HttpServletRequest 這個有點不爽 @RequestMapping("/test") public String test(HttpServletRequest request) { request.setAttribute("test", "你妹"); return "/test"; } }
四、spring 配置文件簡單配置spring-mvc
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 註解掃描包 也就是指定controller 所在的包--> <context:component-scan base-package="com.ly.action" />
<!-- 開啓註解 --> <mvc:annotation-driven />
<!-- 指明前綴和後綴 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>