Spring MVC 学习2:一个非常简单的Spring MVC项目

来源:互联网 发布:centos挂载磁盘 编辑:程序博客网 时间:2024/04/29 14:01

第一步:新建一个DynamicWeb Project项目

第二步:添加jar

我是把Spring所有的Jar包添加到项目中去,如果想了解各个jar包的作用,请关注我稍后的博客,会在博客中详细说明的。

第三步:配置web.xml

<?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"  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  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">  <display-name>springmvc-1</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <!-- 配置DispatcherServlet -->  <servlet>  <servlet-name>springDispatcherServlet</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <!-- 配置DispatcherServlet的第一个初始化参数:配置SpringMVC配置文件的位置与名称 -->  <!-- 实际上也可以不通过contextConfigLocation来配置SpringMVC的配置文件,如果使用   默认的,那么默认的配置文件的路径为:/WEB-INF/<servlet-name>-servlet.xml   本例中默认的配置文件为"/WEB-INF/springDispatcherServlet-servlet.xml" -->  <init-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:springmvc.xml</param-value>  </init-param>  <!--标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。1标示在启动的时候会加载这个Servlet -->  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>springDispatcherServlet</servlet-name>  <!-- "/"代表是处理任何请求都会被应答,也可以指定特别的请求("*.text"表示任何text结尾的请求会被应答) -->  <url-pattern>/</url-pattern>  </servlet-mapping></web-app>

第四步:创建SpringMVC的配置文件springmvc.xml

选中src目录,右击创建Spring Bean Configuration File



此处需要选中三个declarations


点击Finish完成创建

第五步:编写核心代码

Step1:编写请求处理器,new一个普通的类,并且把它声明成Controller

类的内容如下

package com.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;//@Controller的作用,可以使得这么一个普通的类标识成一个Controller@Controllerpublic class HelloWorld {    /**     * 1:使用@RequestMapping注解来映射请求的URL     * 2:返回值会在springmvc.xml中找到对应的视图解析器,通过     *   视图解析器解析为实际的物理视图,对于InternalResourceViewResolver     *   视图解析器来说,会做如下的解析:     *      通过(prefix+返回值+后缀)这样的方式来得到实际的物理视图,然后做     *      转发操作(会转到:/WEB-INF/views/success.jsp页面)     * @return     */    @RequestMapping("/helloworld")    public String hello(){        System.out.println("Hello World");        /*返回的结果会在springmvc.xml中找到对应的视图解析器,          所以需要在springmvc.xml中配置视图解析器*/        return "success";    }}

Step2:编写Controller类方法的应答请求(即JSP页面),在WebContent目录下新建一个jsp页面

页面内容如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    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=ISO-8859-1"><title>Insert title here</title></head><body><!-- 点击超链接,会发出请求,请求会被我们在web.xml中配置的springDispatcherServlet所进行处理 --><a href="helloworld">HelloWorld</a></body></html>

Step3:在SpringMVC的配置文件中配置视图解析器

<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 配置自动扫描的包,只有这样Handler中的注解才会起作用--><context:component-scan base-package="com.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>

第六步:把项目添加到服务器中启动,测试

点击超链接,页面跳转
在控制台输出:

0 0