本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容。

一、项目结构

本项目使用idea intellij创建,配合maven管理。整体的目录结构如图:
其中java文件夹是sources文件夹,resources是资源文件夹。spring文件夹里是Spring上下文配置和Spring MVC配置文件。
 
需要注意的是,项目自动生成以后会有两个web文件目录,一个是web文件夹(我这里已经删除了),另一个是默认的webapp文件夹。我这里使用默认的目录,也就是说webapp文件夹。页面文件都放在该目录里面。
                                                                                         *如图可配置默认的web文件默认路径。*
二、配置文件
maven配置,添加各种依赖
省略,这个不是重点。需要注意的是,添加依赖的时候不能重复添加不同版本的包。
web.xml文件配置
首先,添加spring 上下文配置,指定Spring Context由classpath下的spring文件夹中的app-context.xml提供:
  1. <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/app-context.xml</param-value>
    </context-param>

    然后添加Spring监听器:

  1. <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    接下来配置Spring MVC的dispatherservlet,同时配置该servlet要拦截的URL。

  1. <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 配置要拦截的URL -->
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    最后,配置一个welcom-file-list。

web.xml全部的代码如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
    <web-appxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.4"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <!-- spring context 配置文件 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/app-context.xml</param-value>
    </context-param>
    <!-- spring 监听器配置 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <!--spring 防内存溢出监听器 -->
    <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <!-- spring mvc servlet配置文件 -->
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 配置要拦截的URL -->
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file></welcome-file>
    </welcome-file-list>
    </web-app>

    配置Spring MVC文件

这里主要配置自动注解、mvc资源引用、视图解析器等
代码如下:
  1. <?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <mvc:resources location="/js/" mapping="/js/**"/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <mvc:annotation-driven/>
    <context:annotation-config/>
    <mvc:default-servlet-handler/> <!--添加component扫描,使package下面的注解生效 -->
    <context:component-scan base-package="com.wxspringmvc.controller"/> <!--添加页面视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/page/"/>
    <property name="suffix" value=".jsp"/>
    <property name="contentType" value="text/html;charset=UTF-8"/>
    </bean>
    </beans>

    配置applicationContext.xml

这里主要是提供默认的上下文,不需要额外的配置,直接使用生成的文件就可以。代码就省略了。
要注意的是,applicationContext.xml是一定要配置的。在web.xml文件中如果不配置,系统会自动到WEB-INF目录下寻找。
View和Controller
这里使用一个简单的用户登录,完成后在页面显示用户名和密码的流程来展示。
View:index.jsp
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>请登录</title>
    </head>
    <body>
    <h5>this is index.jsp</h5>
    <form action="/user/index" method="post">
    <p>用户名:</p><input type="text" id="username" name="username">
    <p>密码:</p><input type="password" id="password" name="password">
    <p><input type="submit" value="提交"></p>
    </form>
    </body>
    </html>

    表单使用post的方式提交到/user/index路径。

Controller:UserController.java
  1. @Controller
    @RequestMapping(value = "/user")
    public class UserController {
    @RequestMapping(value = "/index" ,method= RequestMethod.POST)
    public ModelAndView userIndex(String username,String password){
    ModelAndView mav = new ModelAndView("user/success"); mav.addObject("username",username);
    mav.addObject("password",password);
    return mav;
    } }

    这里可以添加一个简单的校验,如果用户名和密码有一个为空,则不能提交:

修改UserController.java的代码如下:
  1. @Controller
    @RequestMapping(value = "/user")
    public class UserController {
    @RequestMapping(value = "/index" ,method= RequestMethod.POST)
    public ModelAndView userIndex(String username,String password){
    ModelAndView mav = new ModelAndView("user/success");
    if(!matchParams( username, password)){
    return new ModelAndView("/index");
    }
    mav.addObject("username",username);
    mav.addObject("password",password);
    return mav;
    } private boolean matchParams(String username,String password){
    if(isEmpty(username)||isEmpty(password))
    return false;
    else
    return true;
    } private boolean isEmpty(String s){
    if(s==null || "".equals(s))
    return true;
    else
    return false;
    }
    }

    View:登录成功界面:success.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>用户首页</title>
    </head>
    <body>
    <p>用户名:${username}</p>
    <p>密码:${password}</p>
    </body>
    </html>

    效果展示

默认页:
输入不正确,提交结果:
输入正确提交:
 
----------------------------------------------------------------------------------------------------------------------------
 
以上就是一个简单的Spring MVC演示了。

Spring MVC篇一、搭建Spring MVC框架的更多相关文章

  1. Spring第一篇【介绍Spring、引入Spring、Spring六大模块】

    前言 前面已经学习了Struts2和Hibernate框架了.接下来学习的是Spring框架-本博文主要是引入Spring框架- Spring介绍 Spring诞生: 创建Spring的目的就是用来替 ...

  2. [Java,MVC] Eclipse下搭建Spring MVC

    转自:http://blog.csdn.net/blue_jjw/article/details/8752466 一.新建Dynamic Web Project 一个web工程最基本的,只看3个地方, ...

  3. 【译文】用Spring Cloud和Docker搭建微服务平台

    by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...

  4. Spring Cloud和Docker搭建微服务平台

    用Spring Cloud和Docker搭建微服务平台 This blog series will introduce you to some of the foundational concepts ...

  5. Spring入门(一):创建Spring项目

    本篇博客作为Spring入门系列的第一篇博客,不会讲解什么是Spring以及Spring的发展史这些太理论的东西,主要讲解下如何使用IntelliJ IDEA创建第一个Spring项目以及通过一个示例 ...

  6. 从零开始学 Java - 搭建 Spring MVC 框架

    没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...

  7. 搭建spring mvc项目

    在之前搭建maven项目这篇的基础上继续集成,引入spring mvc支持 一.添加jar包引用 修改pom.xml文件,加入:(其他关联的jar包maven会自动引用) <!-- 项目属性 - ...

  8. mac os版本Intellij IDEA 搭建spring mvc的maven工程(新手教学)

    由于近期换了新公司,又换mac pro作为新电脑,打算把用了很多年的eclipse换成IDEA(IDEA比eclipse的好处我就不多说了),由于mac os和IDEA刚开始用不久,所以专门用一篇博客 ...

  9. 零基础搭建 spring mvc 4 项目(本文基于 Servlet 3.0)

    作者各必备工具的版本如下: Tomcat:apache-tomcat-7.0.63 (下载链接) Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载 ...

随机推荐

  1. su与su-

    1.Linux中的用户切换:su和su - 的区别 大部分Linux发行版的默认账户是普通用户,而更改系统文件或者执行某些命令,需要root身份才能进行,这就需要从当前用户切换到root用户,Linu ...

  2. NIO及Reactor模式

    关于Nio Java NIO即Java Non-blocking IO(Java非阻塞I/O),是Jdk1.4之后增加的一套操作I/O工具包,又被叫做Java New IO. Nio要去解决的问题 N ...

  3. POJ 2533 Longest Ordered Subsequence LCS O(n*log(n))

    题目链接 最长上升子序列O(n*log(n))的做法,只能用于求长度不能求序列. #include <iostream> #define SIZE 1001 using namespace ...

  4. wget 扒站

    在Linux下,通过一个命令就可以把整个站相关的文件全部下载下来. wget -r -p -k -np [网址] 参数说明: -r : 递归下载 -p : 下载所有用于显示 HTML 页面的图片之类的 ...

  5. 解决springmvc报No converter found for return value of type: class java.util.ArrayList问题

    一.背景 最近闲来无事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的环境(搭建步骤会在以后博客中给出),结果运行程序时,适用@ResponseBody注解进行返回Lis ...

  6. Linux下的DOS攻击

    Linux下的DOS攻击 DOS是Denial of service的简称,即拒绝服务,造成Dos攻击行为被称为Dos攻击,其目的是使计算机或网络无法提供正常的服务.最常见的Dos攻击有计算机带宽攻击 ...

  7. ElasticSearch性能优化官方建议

    ES 手册 如何提高ES的性能 不要返回较大的结果集 ES是设计成一个搜索引擎的,只擅长返回匹配查询较少文档,如果需要返回非常多的文档需要使用Scroll. 避免稀疏 因为ES是基于Lucene来索引 ...

  8. Eclipse中使用Maven创建web项目

    一.创建一个Maven项目 1.Eclipse中用Maven创建项目 上图中点击next 2.继续next 3.选maven-archetype-webapp后,next 4.填写相应的信息,Pack ...

  9. Python-socket网络编程

    一.计算机网络 多台独立的计算机用网络通信设备连接起来的网络.实现资源共享和数据传递.比如,我们之前的学过的知识可以将D盘的一个文件传到C盘,但如果你想从你的电脑传一个文件到我的电脑上目前是做不到的; ...

  10. Python-基础数据类型

    数据类型 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定 ...