今天我们来学习第一个SpringMVC程序

一.配置开发方式

(1)首先建立一个SpringMVC  web程序

(2)导入jar包

(3)建立UserController.java

package com.zk.UserController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; public class UserController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
// 接受请求,接受参数,验证参数
// 封装参数,调用业务方法
// 返回视图
ModelAndView mv=new ModelAndView();
//设置页面回显数据
mv.addObject("hello", "凤姐喜欢你");
//指定跳转视图,返回物理视图
//mv.setViewName("/jsp/index.jsp");
//返回逻辑视图
mv.setViewName("index");
return mv;
} }

(4)配置web.xml,在web.xml配置DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Spring_001</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
Spring 默认加载SpringMVC的配置文件,这个需要满足一下规则:
命名规则:Servlet-name-servlet.xml========>SpringMVC-servlet.xml
路径规范SpringMVC-serlvet.xml必须放在WEB-INF下
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

(5)配置springmvc.xml,放在config文件夹下;

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置处理器映射器,SpringMVC的默认处理器映射器
BeanNameUrlHandlerMapping:根据bean(自定义Contoller)的name属性的url去寻找handler(Action:controller)
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 配置处理器适配器:负责执行Controller,springMVC默认处理器适配器SimpleControllerHandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 配置自定义UserController -->
<bean name="/hello.do" class="com.zk.UserController.UserController"></bean>
<!-- 配置SpringMVC视图解析器:解析逻辑视图
后台返回逻辑视图,
视图解析器解析出真正的物理视图:前端+逻辑视图+后缀=====/WEB-INF/jsps/index.jsp
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

(6)jsp目录下,index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
${hello }
</body>
</html>

运行程序后,输入url:http://ms-20170731tkkc:8080/SpringMVC_001/hello.do,得到运行结果为:

二.注解开发方式

第二种开发方式为注解开发,使用@Controller加@RequestMapping进行注解开发。

代码如下:

UserController.java

package com.zk.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller//<bean id="UserController" class="UserController路径">
public class UserController {
//访问路径注意更改
//请求映射value="/hello.do",
//@RequestMapping("hello")
//@RequestMapping("/hello.do")
//@RequestMapping(value="/hello.do")
//@RequestMapping(value="/hello.do",method=RequestMethod.GET)
@RequestMapping(value="/hello.do",method={RequestMethod.GET,RequestMethod.POST})
public String myHello(){
return "hello";
}
}

requestMapping(value=”/hello.do”,method=RequestMethod.POST)

浏览器直接访问,a标签都是get请求

表单提交(指定post),ajax指定post提交,post提交。

Spring.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.zk.Controller"></context:component-scan> <!-- 配置注解处理器映射器
功能:寻找执行类Controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <!-- 配置注解处理器适配器
功能:调用controller方法,执行controller
-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans> 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Spring_006</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
Spring 默认加载SpringMVC的配置文件,这个需要满足一下规则:
命名规则:Servlet-name-servlet.xml========>SpringMVC-servlet.xml
路径规范SpringMVC-serlvet.xml必须放在WEB-INF下
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'Hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
Hello,UserController <br>
</body>
</html>

  运行结果如下:

SpringMVC_001 第一个SpringMVC程序的更多相关文章

  1. 手把手教你优雅的编写第一个SpringMVC程序

    可能之前写的文章走进SpringMVC世界,从SpringMVC入门到SpringMVC架构中的第一个springMVC入门程序讲解的不是那么优雅.细致.精巧,因此特地写这篇稍微优雅.细致.精巧一些的 ...

  2. SpringMVC-02 第一个SpringMVC程序

    SpringMVC-02 第一个SpringMVC程序 第一个SpringMVC程序 配置版 新建一个Moudle , springmvc-02-hello,确定依赖导入进去了 1.配置web.xml ...

  3. 第一个SpringMVC程序 (配置版)

    通过配置版本的MVC程序,可以了解到MVC的底层原理,实际开发我们用的是注解版的! 1.新建一个普通Maven的项目,然后添加web的支持 2.导入相关的SpringMVC的依赖 3.配置web.xm ...

  4. SpringMVC学习02(我的第一个SpringMVC程序)

    2.Hello,SpringMVC 2.1 配置版 1.新建一个Moudle , springmvc-02-hello , 添加web的支持! 2.确定导入了SpringMVC 的依赖! 3.配置we ...

  5. 1、第一个SpringMVC程序

    1.创建如下项目结构 2.在src下的com.springmvc下创建User.java package com.springmvc; public class User { private Stri ...

  6. 第一个SpringMVC程序 (注解版)

    1.新建一个web项目 2.导入相关jar包 3.编写web.xml , 注册DispatcherServlet <?xml version="1.0" encoding=& ...

  7. 第一个SpringMVC程序(最简单的)

      注册中央调度器,这个中央调度器就是org.springframework.web.servlet.DispatcherServlet这个类(web.xml servlet-name节点的名字,必须 ...

  8. 第一个SpringMVC程序

    1.创建工程 2.导入依赖 3.编写配置文件(web.xml) 配置了SpringMVC的入口: 4.SpringMVC的配置文件 路径:默认情况下,在目录下找这个文件:/WEB-INF/{servl ...

  9. 建立一个简单的SpringMVC程序

    首先,所建立的程序是一个web程序,所以在web.xml文件中进行如下的配置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...

随机推荐

  1. Spark Streaming:updateStateByKey报错 is not applicable for the arguments...

    ones.updateStateByKey(new Function2<List<Integer>, Optional<Integer>, Optional<Int ...

  2. Linux - pip 安装使用说明

    简介 pip类似RedHat里面的yum,安装Python包非常方便.本节详细介绍pip的安装.以及使用方法 方案一 wget https://bootstrap.pypa.io/get-pip.py ...

  3. XSS详解【1】---基本概念和攻击原理

    这节主要讲述XSS的基本概念和攻击原理. 一 XSS基本概念 人们经常将跨站脚本攻击(Cross Site Scripting)缩写为CSS,但这会与层叠样式表(Cascading Style She ...

  4. python之路set

    一.set和其他集合的区别: list :允许重复的集合,修改 tuple:允许重复的集合,不修改 dict:字典 set:不允许重复的集合,set不允许重复的,列表是无序的 1.创建一个set s= ...

  5. 关于gets读入因为缓冲区出现的问题

    今天被一个同学丢了代码求debug 然后发现bug挺有意思的,稍微记录一下 首先我们读入的东西都会被丢进缓冲区等待接收,比如abc\n,如果你使用scanf读入的话,它在读入到\n的时候就会提取它需要 ...

  6. 对malloc和free和数据结构和算法的一些感触

    当年2013.9.大一学c程序设计,因为当时还没有学数据结构,只学了程序设计,大学上的课真的是承上启下的不好,刚学到这里,就断了旋一样,对这个malloc和free一直很迷惑,这些狗玩意是干嘛,因为用 ...

  7. centos7安装谷歌浏览器

    1. 安装: 考虑到国内无法访问Google,所以需要自己配置yum源: 1.在目录 /etc/yum.repos.d/ 下新建google-chrome.repo文件,命令如下: 1.cd /ect ...

  8. Visual detection of structural changes in time-varying graphs using persistent homology

    PKU blog about this paper Basic knowledge:  1. what is time-varying graphs? time-varying graph VS st ...

  9. 融e学 一个专注于重构知识,培养复合型人才的平台【获取考试答案_破解】

    考试系统-融e学-一个专注于重构知识,培养复合型人才的平台.[获取答案] ganquanzhong 背景:今天去完成学校在融e学上开设的必修课和选修课考试,由于自己的时间有限(还有其他的事情要去做). ...

  10. h264 RTP STAP-A单时间聚合包

    参考官方文档:http://www.rosoo.net/Files/UpFiles/RsProduct/avtools/2009-4/2009491562537854.txt 聚合包的RTP荷载格式的 ...