SpringMVC的常见的使用方法

来源:互联网 发布:手机区域截图软件 编辑:程序博客网 时间:2024/06/05 00:20
一.SpringMVC的五个核心组件
1.DispatcherServlet:用于接收所有的请求,并分发给不同的Controller
2.HandlerMapping接口:实现请求与Controller的映射(对应)关系
3.Controller接口:处理请求,后续可能还会访问Model层,并且必须返回ModelAndView类型的结果
4.ModelAndView:Controller处理请求后得到的结果
5.ViewResolver接口:结果与View的对应关系,即什么样的结果交给哪个View显示

二.SpringMVC - HelloWorld - 1
1 创建项目:DAY03-05-SpringMVC-HelloWorld
2 解决web.xml的问题
3 添加依赖spring-webmvc
4 配置Tomcat Runtime
5 在src\main\resource下粘贴spring的配置文件

6 配置web.xml

<servlet>     <!-- Servlet基本配置 -->     <servlet-name>SpringMVC</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <!-- 配置加载spring的配置文件 -->     <init-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath:applicationContext.xml</param-value>     </init-param>     <!-- 启动时加载 -->     <load-on-startup>1</load-on-startup></servlet><servlet-mapping>     <servlet-name>SpringMVC</servlet-name>     <url-pattern>*.do</url-pattern></servlet-mapping>

7.准备测试:新建任何类,例如:cn.tedu.spring.bean.User,并在User的构造方法中添加输出语句

8.测试:将项目部署到Tomcat,然后启动Tomcat,待Tomcat启动完成后,
在Console没有报告致命错误,并且可以看到User的构造方法被执行,则完成!

三.SpringMVC - HelloWorld - 2
1.设计请求
http://SERVER:PORT/PROJECT/hello.do
2.在Spring的XML文件中配置HandlerMapping

<!-- 配置HandlerMapping --><!-- 不需要指定id属性 --><bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    <property name="mappings">        <props>            <!-- key表示请求的路径 -->            <!-- 文本节点配置Controller的bean id -->            <prop key="/hello.do">helloController</prop>        </props>    </property></bean>
3.自定义用于响应的Controller类,例如HelloController,添加@Component注解,保证该组件的bean id与以上步骤配置的是一致的,然后实现Controller接口

4.完成HelloController中的抽象方法,在方法中,添加输出语句,便于观察,然后创建ModelAndView类型的对象,并通过setViewName(String)指定最终响应请求的资源的名称,例如index

5.测试:打开浏览器,输入请求地址,正常结果是显示404,并且在Console可以观察到HelloController已经运行

四.SpringMVC - HelloWorld - 3
1.创建最终显示内容的JSP页面,例如index.jsp,该文件的名称需要与Controller中返回时设置的保持一致
2.在Spring的XML文件中配置ViewResolver
<!-- 配置ViewResolver --><!-- 不需要指定id属性 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    <!-- 前缀,即View文件在项目中相对于webapp的路径 -->    <property name="prefix" value="/WEB-INF/"></property>    <!-- 后缀,即文件的扩展名 -->    <property name="suffix" value=".jsp"></property></bean>
3.测试运行,正确的运行效果是能够成功显示jsp页面。


五.最终的项目如下

访问hello.do

1.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_2_5.xsd" version="2.5">  <display-name>DAY03-05-SpringMVC</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>    <servlet>  <!-- Servlet基本配置 -->  <servlet-name>SpringMVC</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <!-- 配置加载spring的配置文件 -->  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:applicationContext.xml</param-value>  </init-param>  <!-- 启动时加载 -->  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>SpringMVC</servlet-name>  <url-pattern>*.do</url-pattern>  </servlet-mapping></web-app>
2.Spring配置文件applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><context:component-scan base-package="cn.tedu.spring"></context:component-scan><!-- 配置HandlerMapping,不需要指定id属性 --><bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><!-- key表示请求的路径,文本节点配置Controller的bean id --><prop key="/hello.do">helloController</prop></props></property></bean><!-- 配置ViewResolver --><!-- 不需要指定id属性 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 前缀,即View文件在项目中相对于webapp的路径 --><property name="prefix" value="/WEB-INF/"></property><!-- 后缀,即文件的扩展名 --><property name="suffix" value=".jsp"></property></bean></beans>

3.WEB-INF下的index.jsp文件

<%@ 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=UTF-8"><title>SpringMVC-HelloWorld</title></head><body><h3>欢迎使用SpringMVC!</h3></body></html>
4.用户类
package cn.tedu.spring.bean;import org.springframework.stereotype.Component;@Componentpublic class User {public User() {System.out.println("User()");}}

5.控制器类

package cn.tedu.spring.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Component;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;@Componentpublic class HelloController implements Controller{public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {//测试输出System.out.println("HelloController.handleRequest");//准备返回值ModelAndView result = new ModelAndView();//设置响应Viewresult.setViewName("index");// 返回return result;}}


原创粉丝点击