Spring mvc 注解方式实现
第一步: web.xml中 启动spring mvc 模块
第二步: 编写普通的pojo类
注意:@Controller和@RequestMapping
第三步 配置main-servlet.xml
[b]spring 跳转到/WEB-INF/目录下的页面,
不要把页面建在/webRoot下了[/b]
第一步: web.xml中 启动spring mvc 模块
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
第二步: 编写普通的pojo类
package com.chapter2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author
* 基于注解的 Controller
*/
@Controller
@RequestMapping("/ok.do")
public class MyController {
@RequestMapping
public String listTurn()
{
return "123";
}
}
注意:@Controller和@RequestMapping
第三步 配置main-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">
<!-- 1,扫描web包中的所有类以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.chapter2"/>
<!--2,启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 3,对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp"/>
</beans>
[b]spring 跳转到/WEB-INF/目录下的页面,
不要把页面建在/webRoot下了[/b]