学习SpringMVC——从HelloWorld开始

来源:互联网 发布:淘宝店铺设置多个客服 编辑:程序博客网 时间:2024/06/14 10:02

学习SpringMVC——从HelloWorld开始

什么是Spring MVC
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。

今天先从写一个Spring MVC的HelloWorld开始,让我们看看如何搭建起一个Spring mvc的环境并运行程序。

配置文件及编写代码

  web.xml(WEB-INF下)
  

<?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"    id="WebApp_ID" version="2.5">    <!-- 配置DispatchcerServlet -->    <servlet>        <servlet-name>springDispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- 配置Spring mvc下的配置文件的位置和名称 -->        <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>springDispatcherServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

Springmvc.xml(scr下)

  在src目录下新建springmvc.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: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.jackie.springmvc"></context:component-scan>        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">            <property name = "prefix" value="/WEB-INF/views/"></property>            <property name = "suffix" value = ".jsp"></property>        </bean></beans>

 HelloWorld.java(com.jackie.springmvc.handlers下)
 

package com.jackie.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloWorld {    /**     * 1. 使用RequestMapping注解来映射请求的URL     * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析     * 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图,然后会转发操作     * "/WEB-INF/views/success.jsp"     * @return     */    @RequestMapping("/helloworld")    public String hello(){        System.out.println("hello world");        return "success";    }}index.jsp(WebContent下)  在新建success.jsp之前,我们需要有一个入口,也就是这里的index.jsp  

<%@ page language=”java” contentType=”text/html; charset=UTF-8”
pageEncoding=”UTF-8”%>




Insert title here

hello world


success.jsp(WEB-INF/views下)

  该页面是作为请求成功后的相应页面
  

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!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=ISO-8859-1"><title>Insert title here</title></head><body><h4>Success Page</h4></body></html>success.jsp