`

Spring4MVC之Helloworld

 
阅读更多

 

SpringMVC 概述

 

-Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一

-Spring3.0 后全面超越 Struts2,成为最优秀的 MVC 框架

-Spring MVC 通过一套 MVC 注解,让 POJO 成为处理请求的控制器,而无须实现任何接口

-支持 REST 风格的 URL请求

-采用了松散耦合可插拔组件结构,比其他 MVC 框架更具扩展性和灵活性

 

第一个SpringMVC程序

 

工程目录结构

 



步骤:

-加入 jar 包

-在 web.xml 中配置 DispatcherServlet

-加入 Spring MVC 的配置文件

-编写处理请求的处理器,并标识为处理器

-编写视图

 

1. 加入 jar 包

 

commons-logging-1.1.3.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

 

2. 在 web.xml 中配置 DispatcherServlet

 

配置 DispatcherServlet :DispatcherServlet 默认加载 /WEB-INF/${servletName}-servlet.xml 的 Spring 配置文件,启动 WEB 层的 Spring 容器。可以通过 contextConfigLocation 初始化参数自定义配置文件的位置和名称。${servletName}表示在web.xml中配置DispatcherServlet这个serlvet时,标签<servlet-name></servlet-name>的值。

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
  version="3.0"
  metadata-complete="true">
  
  <display-name>Spring4MVC</display-name>
  
  <!-- 配置 DispatcherServlet -->
  <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.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>
    
  <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
  
</web-app>

 

3. 加入 Spring MVC 的配置文件

 

<?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="org.rabbitx.web.spring4mvc.helloworld"></context:component-scan>

    <!--配置视图解析器:如何把handler方法返回值解析为实际的物理视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/pages/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>

 

 

4. 编写处理请求的处理器,并标识为处理器

 

package org.rabbitx.web.spring4mvc.helloworld;

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

@Controller
public class HelloworldController {
	
	private final static String SUCCESS = "success";
	
	private final static String FAILURE = "failure";
	
	@RequestMapping("/say-hello")
	public String sayHello()
	{
		System.out.println("---------sayHello--------");
		if(System.currentTimeMillis()%2 == 0)
		{
			return SUCCESS;
		}
		else
		{
			return FAILURE;
		}
	}
}

 

5. 编写视图

 

<%@ 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>Helloword</title>
</head>
<body>
    <hr/>
    <p>第一个Spring MVC程序:</p>
    <a href="say-hello">Spring4MVC-Hellowrold</a>
    <hr/>
</body>
</html>

  

<%@ 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>SUCCESS</title>
</head>
<body>
    <hr/>
    
    <h1>SUCCESS</h1>
    
    <hr/>
    <a href="/org.rabbitx.web.spring4mvc/helloworld.jsp">返回首页</a>
</body>
</html

 

<%@ 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>FAILURE</title>
</head>
<body>
    <hr/>

    <h1>FAILURE</h1>
    
    <hr/>
    <a href="/org.rabbitx.web.spring4mvc/helloworld.jsp">返回首页</a>
</body>
</html>

 

 测试

 

1. 在浏览器中输入:http://localhost:8080/org.rabbitx.web.spring4mvc/helloworld.jsp

 


 

 

  • 大小: 31.4 KB
  • 大小: 20.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics