在Eclipse安装好Spirng插件后,创建一个动态Web工程项目
项目目录如下:Tomcat为服务器、在类路径下的spring类型的XML文件、相应的Servlet类(此处为handlers包下类)、
web.xml配置文件和index.jsp文件为自动生成。
第一步:将类路径下spring类型XML文件配置进web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
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">
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置DispatcherServlet的初始化参数,此处配置springXMl文件ID和文件路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 以下表示解析器文件路径在类路径下的springmvc文件 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- 在这配置哪些请求可以给到Servlet -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第二步:编写相应的Handlers类(即Web的Servlet部分),来对网页请求数据进行相应的处理。
1、需要将解析类加入SpringIOC容器:在类前加@Controller注解 ,同时在springmvc.xml文件里使用自动装配。
2、@RequestMapping注解加在类、或方法前,网页超链接或表单会根据该注解值返回对应类的对应方法(即JSP
请求页面如超链接需要使用“类名/方法名”方式设置href属性)。
3、若有返回值,则会根据springmvc文件里的前缀与后缀、返回值合成路径转发至相应页面
package com.handlers;
import java.io.Reader;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.beans.User;
/*
* 需要将解析类加入SpringIOC容器:1、加@Controller注解 2、在springmvc.xml文件里使用自动装配
*
* @RequestMapping注解用來映射请求的URL,可以加在类前、方法前,一般为超链接的href属性
* 返回值会通过视图解析器解(在springmvc.xml里配置,为什么是类路径下的springmvc.xml文件?因为在web.xml里配置好了)
* 析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器,
* 会做如下的解析:
* 通过 prefix + 方法返回值 + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
*
* /WEB-INF/views/success.jsp
*/
@RequestMapping("/HelloWorld")
@Controller
public class HelloWorld {
@RequestMapping("/helloworld")
public String hello() {
System.out.println("HelloWorld!");
return "success";
}
}
springmvc文件里使用spring自动扫描,同时使用<bean>配置视图解析器,来合成转发页面的路径
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置自定扫描的包 -->
<context:component-scan base-package="com.handlers"></context:component-scan>
<!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图,转发至相关页面-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 在实际开发中通常都需配置 mvc:annotation-driven 标签 -->
<!-- <mvc:annotation-driven></mvc:annotation-driven> -->
</beans>
JSP页面 使用超链接或表单,如href属性、action属性需设置为以下格式:
类前@RequestMapping注解值/方法前@RequestMapping注解值
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="helloworld">HelloWorld1:不加类注解</a>
<br/>
<a href="HelloWorld/helloworld">HelloWorld2:加类注解</a>
<br/>
</body>
</html>
注意:@RequestMapping注解值(即value属性值)加不加都行
JSP请求路径:注解了类名和方法名情况下类名前不加方法名前加,只注解了方法名情况下可不加/