绿色版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. [js高手之路]Node.js+jade+mongodb+mongoose实现爬虫分离入库与生成静态文件

    接着这篇文章[js高手之路]Node.js+jade抓取博客所有文章生成静态html文件继续,在这篇文章中实现了采集与静态文件的生成,在实际的采集项目中, 应该是先入库再选择性的生成静态文件.那么我选 ...

  2. 软件测试的cookie测试

    1.什么是cooike测试 Cookie是指网站用于辨别身份,进行会话(session)跟踪而存储在客户端的数据.它是有服务器产生并发送给客户端的.其用途是提供一个方便的功能以简化用户输入,节省访问页 ...

  3. @SuppressWarnings抑制警告

    @SuppressWarnings(“XXXX”) 来抑制编译时的警告信息.参数如下: 关键字 用途 all to suppress all warnings boxing  to suppress ...

  4. 开发中mysql和oracle的区别

    首先就不描述mysql与oracle在整个数据库系统上的区别了,仅从程序员开发的角度来说: 1.主键: mysql一般会用到一个自增的属性,例如设置一个id字段,类型设置为auto increment ...

  5. Mybatis——choose, when, otherwise可以达到switch case效果

    在mapping文件中实现动态sql,如果想达到if else的效果可以使用:choose, when, otherwise <choose> <when test="ti ...

  6. PHP浮点型(float)转换为整形(int)/round()保留小数点后几位

    round(x,y); x:需要转换的变量 y:保留几位小数 <?php echo round(3.112312321) //输出3 echo round(3.112312321,3) //输出 ...

  7. vue数组语法兼容问题

    先来一行代码: <a :href="['NewsNote.asp?ID='+item.ID+'&MenuType=C']" v-text="item.Tit ...

  8. 程序员网站开发时应该注意的SEO问题

    一.链接的统一性 搜索引擎排名最主要的因素就是网站内容和链接,假如网站内部链接不一致,在很大程度上直接影响着网站在搜索引擎中的排名.例如彩票专营店导航栏中的“首页”链接,程序员在开发时可能会有以下几种 ...

  9. 我从.net转别的语言的遭遇,现在貌似又要回头(一)

    从2016年开始,作为公司最后的一个.net部门,被迫转向了php. php里面装得一手什么逼呢? 首先,你要来几本入门的书,linux,mysql,php基础语法,nginx或apache.哥那时候 ...

  10. canvas图表详解系列(1):柱状图

    本章建议学习时间4小时 学习方式:详细阅读,并手动实现相关代码(如果没有canvas基础,需要先学习前面的canvas基础笔记) 学习目标:此教程将教会大家如何使用canvas绘制各种图表,详细分解步 ...