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 ...
随机推荐
- Learning Cocos2d-x for WP8(8)——动作Action
原文:Learning Cocos2d-x for WP8(8)--动作Action 游戏很大程度上是由动作画面支撑起来的. 动作分为两大类:瞬间动作和延时动作. 瞬间动作基本等同于设置节点的属性,延 ...
- 百度经纬度和google经纬度互转
原文:百度经纬度和google经纬度互转 百度地图的坐标转换,由于百度地图在GCJ02协议的基础上又做了一次处理,变为 BD09协议的坐标,以下是坐标的转化方式,可以方便和其他平台转化 private ...
- Hibernate主键生成策略简单总结
数据库表主键的知识点: Generator 为每个 POJO 的实例提供唯一标识. 一般情况,我们使用"native".class 表示采用由生成器接口net.sf.hiberna ...
- I深搜
<span style="color:#330099;">/* I - 深搜 基础 Time Limit:1000MS Memory Limit:10000KB 64b ...
- 二分图行列匹配---> hdu2119,hdu1498
hdu2119 题意:给定一个矩形方格,每个格子里面的数字是0或者1,每次操作可以把一整行或列的1变成0,问最少多少次操作能将1全部变为0 一次可以消除某一行或者某一列的1但是可以这么想,最多有多少个 ...
- 【译】手动处理Team Foundation Server 2010 数据仓库和分析服务数据库
原贴地址:Manually Processing the Team Foundation Server 2010 Data Warehouse and Analysis Services Databa ...
- CTR校准
普遍预测CTR不准,需要校准.例如.boosted trees and SVM预測结果趋于保守.即预測的概率偏向于中值:而对于NaiveBayes预測的概率,小概率趋于更小.大概率趋于更大.经常使用的 ...
- css3仿山猫侧边栏
演示:http://jsfiddle.net/Adce2/ 其主要思想: 1, 先画边栏html. 2, 使用css3分别财产close sidebar-content动图片. 3, 使用css3的k ...
- 入门git
入门git 0x01前言 既然没有华丽的出场,那就平凡的分享,首先我要说明一点本篇文章针对Git初学者,对我自己学Git的资源的整合,其实本篇索引应该在我写Git系列文章的时候就紧跟着放上索引的, ...
- HDU ACM 1071 The area 定积分计算
分析: 1.求抛物线方程F(x)=a*x^2+b*x+c: 2.求直线方程f(x)=k*x+b. 3.利用定积分计算F(x)-f(x)在x2到x3之间的面积. #include<iostream ...