Spring MVC在使用annotation進行配置controller時候要注意:兩種urlmapping的模式不能同時使用,如果使用annotation就不能再配置urlmapping了。
<action>-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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 對com.founder.action包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 -->
<context:component-scan base-package="com.founder.action"/>
<!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!--把請求的URL映射到Controller的name上面 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
package com.founder.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
@Controller
@RequestMapping
public class HelloController{
@RequestMapping
public String world(){
String name = "Eric";
System.out.println("name is = " + name);
return "hello";
}
@RequestMapping
public String test() {
String name = "test";
System.out.println("name is = " + name);
return "test";
}
}