绿色版Spring MVC(单纯的springMVC)

  一、导包,为了获取请求数据多添加一个

  

  二、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"
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>webSpringMVC</display-name> <!-- 加载spring容器 -->
<!-- 初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param> <!-- 监听 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <!-- springmvc前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/springMvc.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping> <!-- 配置过滤器 -->
<!-- post乱码过虑器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 首页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- session超时 -->
<!--分钟为单位
设置为0,-1 表示永不超时 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>

  三、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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 启用 spring 注解 -->
<context:component-scan base-package="test" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀)
比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp” -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

  四、applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> </beans>

   四、hello.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>SpringMVC</title>
</head>
<body>
您好,${user }!
</body>
</html>

  五、Controller

package test;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; @Controller
//可以配置也可以不配 多一级目录
//@RequestMapping("/hello")
public class HelloController{ @SuppressWarnings("deprecation")
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
// 添加模型数据 可以是任意的POJO对象
mv.addObject("user", request.getParameter("user") + "-->" + new Date().toLocaleString());
// 设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
mv.setViewName("hello");
return mv;
} @SuppressWarnings("deprecation")
@RequestMapping("/hello")
public String hello(HttpServletRequest request,HttpServletResponse response){
request.setAttribute("user", request.getParameter("user") + "-->" + new Date().toLocaleString());
return "hello";
} }

  六、tomcat部署好后访问地址

 http://localhost:8080/springMvc/hello.action?user=SpringMMM

 代码见 https://github.com/fangxiongwei007/springMvc

spring MVC 环境搭建的更多相关文章

  1. Spring MVC 环境搭建(二)

    在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...

  2. Spring MVC 环境搭建(一)

    一.建立 JavaWeb 项目 1.建立一个 Java 项目. 2.在项目下新建一个文件夹 webapp (命名可自取,这个目录即是网站根目录),再在该文件夹下新建一个 WEB-INF 文件夹(命名固 ...

  3. Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)

    Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...

  4. Java学习笔记之:Spring MVC 环境搭建

    一.创建项目 1.新建java动态项目 2.在web-inf/lib文件夹下导入jar 3.创建所需要的包和文件 二.搭建SpringMVC 1.配置web.xml(WEB-INF下) <?xm ...

  5. [Spring MVC] - Spring MVC环境搭建

    1) 复制Spring所需要的lib包         (这是SSH所需要的lib包,如果只使用spring,可以移除一些包) 2) 配置web.xml <?xml version=" ...

  6. spring MVC环境搭建

    1.新建web项目,并在web.xml加入spring mvc的servlet <!-- spring mvc容器和servlet的定义 --> <servlet> <s ...

  7. Spring MVC环境搭建和配置

    1. 创建Dynamic web project 2. 修改WEB-INF/web.xml,内容如下: <?xml version="1.0" encoding=" ...

  8. Spring MVC: 环境搭建并实现简易的HelloWorld

    第一步:使用配置Tomcat服务器的Eclipse新建一个名为“TestSpringMVC”的web项目 第二步:将所使用的jar包复制到WEB-INF/lib目录下 第三步:在web.xml中配置D ...

  9. spring mvc 框架搭建及详解

    现 在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不 ...

随机推荐

  1. 201521123014 《Java程序设计》第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 1.关于异常上次作业已经概括得差不多了,创建自己的异常时可以使用Java类库中已经定义好的类,也可自定义异常 ...

  2. TCP/IP协议:OSI七层模型、TCP/IP四层模型的对比

    1. OSI七层和TCP/IP四层的关系 1.1 OSI引入了服务.接口.协议.分层的概念,TCP/IP借鉴了OSI的这些概念建立TCP/IP模型. 1.2 OSI先有模型,后有协议,先有标准,后进行 ...

  3. 多线程面试题系列(2): CreateThread与_beginthreadex本质区别

    本文将带领你与多线程作第一次亲密接触,并深入分析CreateThread与_beginthreadex的本质区别,相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与_beg ...

  4. JAVA多线程高并发学习笔记(三)——Callable、Future和FutureTask

    为什么要是用Callable和Future Runnable的局限性 Executor采用Runnable作为基本的表达形式,虽然Runnable的run方法能够写入日志,写入文件,写入数据库等操作, ...

  5. pygame 弹力球及其变速的实现

    期望: 1.球体接触到框体后反弹 2.设置速度按键,按下后改变球体速度.颜色状态 具体实现: import pygame from pygame.locals import * import sys, ...

  6. Python学习笔记009_构造与析构

    >>> # 魔法方法>>> >>> # 魔法方法总是被双下划线包围,例如 __init__>>> # 魔法方法是面向对象的Pyt ...

  7. 作为一个C#程序员, 你应该上手Kotlin

    Kotlin最近火了, 在Google IO大会 Kotlin宣布Kotlin将会成为Android官方开发语言之后, Kotlin这样一个JVM上的新*(其实从诞生到现在已经有5年历史的)语言. 终 ...

  8. 初识Hibernate之环境搭建

         相信所有做后端的程序员同行们,没有不知道Hibernate大名的.这是一个经典的轻量级Java EE持久层的解决方案,它使得我们程序员能以面向对象的思维操作传统的关系型数据库,这也是其存在的 ...

  9. this的四种绑定形式

    一 , this的默认绑定 当一个函数没有明确的调用对象的时候,也就是单纯作为独立函数调用的时候,将对函数的this使用默认绑定:绑定到全局的window对象. 一个例子 function fire ...

  10. css左右布局的几种实现方式和优缺点

    记录一下左右布局的实现方式,实现的具体效果是,左侧固定宽度,高度适中等于父元素的高度,父元素的高度由右侧内容决定: html代码如下: <div class="parent" ...