欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC快速入门(XML版本)

程序员文章站 2022-07-15 11:11:08
...

1springmvc快速入门(传统版)
   1:创建springmvc-day01这么一个web应用
   2:导入springiocspringweb , springmvc相关的jar
SpringMVC快速入门(XML版本)
 3:在/WEB-INF/下创建web.xml文件

<!-- 注册springmvc框架核心控制器 -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
 4.创建HelloAction.java控制器类
/**
 * 控制器
 * @author AdminTC
 */
public class HelloAction implements Controller{
	/**
	 * 业务方法
	 */
	public ModelAndView handleRequest(HttpServletRequest requqest,HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message","这是我的第一个springmvc");
		modelAndView.setViewName("/jsp/success.jsp");
		return modelAndView;
	}
}
 5.在/WebRoot/下创建jsp/success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>这是我的第一个springmvc应用程序</title>
  </head>
  <body>
	success.jsp<br/>
	${message}
  </body>
</html>

  6.在/WEB-INF/创建DispatcherServlet-servlet.xml配置文件,xml头部信息与spring.xml相同
注意:该配置文件的命名规则:web.xml文件中配置的<servlet-name>的值-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:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
	  xmlns:mvc="http://www.springframework.org/schema/mvc"
      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-3.0.xsd
 	  
	  http://www.springframework.org/schema/aop 
	  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	  
	  http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        
      ">
      
      <!-- 控制器(程序员) -->
    <bean name="/hello.action" class="cn.itcast.javaee.springmvc.base.HelloAction"></bean>  
    
    
    <!-- 映射器(框架) -->  
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>  
      
      
    <!-- 适配器(框架) -->  
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>  
      
      
    <!-- 视图解析器(框架) -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>  
      
      
</beans>
    7.部署web应用到tomcat中,通过浏览器访问如下URL http://127.0.0.1:8080/springmvc-day01/hello.action


一、加载自定义目录下的springmvc.xml配置文件

默认情况下:springmvc框架的配置文件必须叫<servlet-name>-servlet.xml且必须放在/WEB-INF/目录下,我们可以在web.xml文件中,为DispatcherServlet配置一个初始化参数,让它去我们指定的目录下加载springmvc.xml配置文件,web.xml代码如下:

	<!-- 注册springmvc框架核心控制器 -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/classes/cn/itcast/javaee/springmvc/config/springmvc.xml</param-value>	
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

如果springmvc.xml配置文件放在src目录下,web.xml代码如下:

	<!-- 注册springmvc框架核心控制器 -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring.xml</param-value>	
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
二、视图解析器InternalResourceViewResolver【解析视图逻辑名对应的真实路径】

ModelAndView对象中即可以封装真实视图路径名,也可以封装视图路径的逻辑名,springmvc.xml 代码如下:

    <!-- 视图解析器(框架) -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/jsp/"/>
    	<property name="suffix" value=".jsp"/>
    </bean>

Action类代码如下:

modelAndView.setViewName("success");