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
相关推荐
spring-mvc-helloworld 可以通过jetty启动,访问 http://localhost:8010/spring-mvc-helloworld/hello/world
这是spring MVC的HelloWorld完整实例,包含jar包
spring4-mvc-gradle-xml-hello-world, Gradle + spring 4 MVC Hello World 示例( XML ) Gradle --spring-4 MVC Hello Worldspring 4 MVC + JSP视图+ XML配置模板,使用Gradle构建工具。使用的###1. 技术Gradle 2.0...
spring3 MVC 入门hello world源码,在eclipse中可以直接运行
采用spring3.0 mvc 注解的方式,在页面显示helloworld
Spring MVC 实例 - Hello World,spring mvc 入门Spring MVC 实例 - Hello World,spring mvc 入门
spring-mvc的一个helloworld的demo,需要的拿去用
我自己写的Spring MVC的一个HelloWold,从建HelloWorld工程,搭建环境Install spring,配置web.xml,配置SpringMVC的配置文件springDispatcherServlet-servlet.xml,添加控制器类,建jsp页面,部署 Tomcat,最后运行...
实现spring mvc 之helloworld
用Spring MVC+hibernate 写的一个HelloWorld小实例,加入了一个简单验证。
Spring MVC3.05,helloworld实例。含完整jar包。
Spring3MVC-REST-HelloWorld 实例简单代码
maven下的spring mvc 【hello world】案例
spring mvc helloworld myeclipse2013中的实例,加说明。
spring mvc helloworld 代碼,導入即可使用,使用Lib庫
基于xml配置的spring mvc Helloworld实例。
spring mvc 在 intellij 的 helloworld 基本配置,以及简单的mvc资源映射
spring5.0的mvc项目实例,HelloWorld实例,基于eclipse oxygen jee 的maven project
NULL 博文链接:https://jiaozhiguang-126-com.iteye.com/blog/1843178
该例子很好的展示了spring4.0的MVC的一个helloworld 例子