第一个SpringMVC程序

1、SpringMVC的开发步骤

(1)加入jar包

(2)在web.xml文件中配置DispatcherServlet

(3)加入SpringMVC的配置文件

 A、配置自动扫描的包。

 B、配置视图解析器。

(4)编写处理请求的处理器,并标识为处理器。

(5)编写视图。

2、第一个SpringMVC程序

(1)加入jar包
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
commons-logging-1.1.1.jar

 

(2)在web.xml文件中配置DispatcherServlet

<?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>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置SpringMVC配置文件的位置和名称 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
</web-app>

【说明】:
1、如果没有配置contextConfigLocation,则默认加载SpringMVC的文件路径和文件名为:/WEB-INF/<servlet-name>-servlet.xml。
2、contextConfigLocation的值也可以为:classpath:springmvc.xml,此时需要将springmvc.xml放到src目录下。

(3)加入SpringMVC的配置文件dispatcher-servlet.xml
 A、配置自动扫描的包。
 B、配置视图解析器。
  
<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="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
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	
	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.springmvc" />	
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/page/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>
(4)编写处理请求的处理器,并标识为处理器
package com.springmvc.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 请求控制器 
 */
@Controller
public class HelloWorldController {

	/**
	 * 1、使用 @RequestMapping 注解来映射请求的  URL
	 * 2、返回值会通过 InternalResourceViewResolver 视图解析器解析为实际的物理视图:
	 *   【格式】: prefix + 返回值 + suffix
     *   【返回】:/WEB-INF/page/success.jsp 
	 */
	@RequestMapping("/helloworld")
	public String hello() {
		System.out.println("Hello World");
		return "success";
	}
	
}

(5)编写返回视图

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>返回页面</title>
  </head>
  <body>
    <h2>Success Page</h2>
  </body>
</html>

  

 (6)项目结构截图

 

 (7)使用的Spring版本

 

猜你喜欢

转载自lipiaoshui2015.iteye.com/blog/2253165