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


<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>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
接下来配置Spring MVC的dispatherservlet,同时配置该servlet要拦截的URL。

<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。

<?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文件

<?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

<%@ 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
@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;
} }
这里可以添加一个简单的校验,如果用户名和密码有一个为空,则不能提交:

@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

<%@ 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 框架搭建及详解
现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...
- Spring MVC 框架搭建及具体解释
如今主流的Web MVC框架除了Struts这个主力 外.其次就是Spring MVC了,因此这也是作为一名程序猿需要掌握的主流框架.框架选择多了.应对多变的需求和业务时,可实行的方案自然就多了. 只 ...
- Spring MVC框架搭建及其详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Java Spring MVC框架搭建(一)
环境准备 >>>>>>java JDK和tomcat,eclipse 1.创建项目 2.项目名称自定义,这边为demo 3.我们已经创建完一个动态网站的项目,还得下 ...
- Spring MVC篇一、搭建Spring MVC框架
本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij创建,配合maven管理.整体的目录结构如图: 其中ja ...
- 从零开始学 Java - 搭建 Spring MVC 框架
没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...
- 如何搭建Spring MVC 框架---Hello World
传送门 现在的Web框架基本都采用了MVC(model-view-Controller)设计模式,其中,Servlet和Filter都可以充当控制器.Spring MVC采用一个Servlet作为控制 ...
- 十七、IntelliJ IDEA 中的 Maven 项目初体验及搭建 Spring MVC 框架
我们已经将 IntelliJ IDEA 中的 Maven 项目的框架搭建完成.接着上文,在本文中,我们更近一步,利用 Tomcat 运行我们的 Web 项目. 如上图所示,我们进一步扩展了项目的结构, ...
- Spring MVC 框架的架包分析,功能作用,优点
由于刚搭建完一个MVC框架,决定分享一下我搭建过程中学习到的一些东西.我觉得不管你是个初级程序员还是高级程序员抑或是软件架构师,在学习和了解一个框架的时候,首先都应该知道的是这个框架的原理和与其有关j ...
随机推荐
- LAMP配置参考地址
http://www.linuxidc.com/Linux/2014-07/104563.htm
- js实现的侧边栏展开收缩效果
原文地址:http://www.softwhy.com/forum.php?mod=viewthread&tid=12246 <!DOCTYPE html> <html> ...
- MyBatis一级缓存引起的无穷递归
MyBatis一级缓存引起的无穷递归 引言: 最近在项目中参与了一个领取优惠劵的活动,当多个用户领取同一张优惠劵的时候,使用了数据库锁控制并发,起初的设想是:如果多个人同时领一张劵,第一个到达的人领取 ...
- twitter 监测登陆活动
http://vicenteaguileradiaz.com/download/tinfoleak/tinfoleak-1.2.tar.gz
- 日历的问题C语言,C++(boost),python,Javascript,Java和Matlab实现
今天看到一个很有意思的话题,例的标题叙述性描述,下面: 根据以下信息来计算1901年1月1至2000年12月31适逢星期日每个月的第一天的合伙人数量? a) 1900.1.1星期一 b) 1月,3 ...
- NET MVC异常处理模块
一个简单的ASP.NET MVC异常处理模块 一.前言 异常处理是每个系统必不可少的一个重要部分,它可以让我们的程序在发生错误时友好地提示.记录错误信息,更重要的是不破坏正常的数据和影响系统运行. ...
- DBMS_STATS.GATHER_TABLE_STATS
由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至关重要! 作用:DBMS_STATS.GATHER_TABLE_STATS统计表,列,索引的统计信息(默认参数下是对表进行直方图信 ...
- Sencha Architect 2 的使用
俗话说的好, 工欲善其事必先利其器, 用Sencha开发的语言, 自己可能不太熟悉, 写出来很麻烦, 于是给大家介绍一个工具. 启动程序第一个界面: 单击第一个Go按钮, 创建一个项目.进入以后, 单 ...
- Eclipse正确导入第三方project
前言 昨晚,在不同的Android做出最终的在线测试时间,在其他平台上正常升级的提示突然报告出来"java.lang.NoClassDefFoundError"误.拉什adb lo ...
- 国内三大PTPrivate Tracker站分析
除这一行外,下面全部内容都是转载.出处不明. 国内三大PT(Private Tracker)站分析 先郑重的声明一下:本文以下的内容所有是复制粘贴的,不代表老夫的观点. 事实上内容我也没细致看. 贴这 ...