1.创建SpringMVC项目

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup></load-on-startup>
</servlet> <!--修改为"/"捕获所有的URL请求-->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

配置dispatcher-servlet.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: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/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.test.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 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

控制类

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; /**
* Created by weihu1 on 2018/8/22 11:46
*/
@Controller
@RequestMapping("mvc")
public class WelcomeController { @RequestMapping("/hello")
public String hello(){
return "welcome";
}
}

新建jsp

<%--
Created by IntelliJ IDEA.
User: weihu1
Date: //
Time: :
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
hello
</body>
</html>

RequestMapping

requestMapping(“hello”)

requestMapping(“/hello.do”)

requestMapping(value=”/hello.do”)

    @RequestMapping("/hello")
public String hello(){
return "welcome";
}

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

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

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

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

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

RequestMaping根路径

@RequestMapping(”/user”)

UserController{

requestMapping(“save”)

Save()

requestMapping(“update”)

Update{}

requestMapping(“find”)

Fiind()

}

项目名/user/save.do

@RequestMapping(”/items”)

ItemsController{

requestMapping(“save”)

Save()

requestMapping(“update”)

Update{}

requestMapping(“find”)

Fiind()

}

项目名/items/save.do

自定义根路径

封装参数

分析接受参数类型:

基本类型,int,String等等基本类型。

Pojo类型

包装类型

Springmvc默认支持类型:

HttpSession,HttpRequstServlet,Model等等。

Struts2参数:基于属性封装。

Springmvc参数封装:基于方法进行封装。

基本类型

需求

封装int类型参数

页面

页面传递参数都是字符串。

接受参数方法

接受字符串类型

页面

代码

接受数组

分析:批量删除:checkbox复选框。Value必须有值。

页面

代码

接受Pojo

页面

代码

接受包装类型参数

userCustom{

private user user;

private List<User> userList;

private Map<K,V> maps;

private items items;

定义UserCustom

页面

代码

接受集合类型参数

接受list集合

代码:

接受map

页面

代码

有了struts2,为什么还需要sprigmvc?

实现机制:

Struts2是基于过滤器实现的。

Springmvc基于servlet实现。Servlet比过滤器快。

运行速度:

Struts2是多列

请求来了以后,struts2创建多少个对象:

ActionContext,valuestack,UserAction,ActionSuport,ModelDriven

userAction里面属性:User对象,userlist集合等

Springmvc是单列。

参数封装来分析:

Struts基于属性进行封装。

Springmvc基于方法封装。

页面回显

查询所有

@RequestMapping("list")
public String list(Model model){
//model 相当于application域对象 List<User> userList = new ArrayList<User>(); User user1 = new User();
user1.setId();
user1.setSex("男");
user1.setUsername("张山峰");
user1.setAddress("武当山");
user1.setBirthday(new Date()); User user2 = new User();
user2.setId();
user2.setSex("男2");
user2.setUsername("张山峰222");
user2.setAddress("武当山222");
user2.setBirthday(new Date()); User user3 = new User();
user3.setId();
user3.setSex("男3");
user3.setUsername("张山峰333");
user3.setAddress("武当山333");
user3.setBirthday(new Date()); userList.add(user1);
userList.add(user2);
userList.add(user3); model.addAttribute("userList", userList); return "list"; }

页面获取

修改

修改代码

回显

URL模版映射

url模版映射可以restfull软件架构。

url模版映射过程

Restfull风格设计

Web.xml拦截方式:在rest目录下所有请求都被拦截,servlet可以拦截目录。

{}:匹配接受页面Url路径参数

@Pathariable:{}里面参数注入后面参数里面

转发和重定向

转发

关键字:forward

本类进行转发:

本类方法与方法之间进行forward

转发方式:

方式一:return ”forward:list.do“;

代码:

重定向

关键字:redirect

本类进行重定向:

本类方法与方法之间进行redirect

重定向方式:

方式一:return ”redirect:list.do“;

方式二:return ”redirect:/user/list.do“;

跨类进行重定向:

转发方式:return ”redirect:/items/list.do“;

2.SpringMVC注解开发的更多相关文章

  1. SpringMVC注解开发初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  2. Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...

  3. springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定

    springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...

  4. springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定

    springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定 标签: springmvc springmvc学习笔记12-springmvc注解开发之包装类型參数绑定 需求 实现方 ...

  5. springmvc学习笔记(10)-springmvc注解开发之商品改动功能

    springmvc学习笔记(10)-springmvc注解开发之商品改动功能 标签: springmvc springmvc学习笔记10-springmvc注解开发之商品改动功能 需求 开发mappe ...

  6. Spring+SpringMVC+MyBatis深入学习及搭建(十五)——SpringMVC注解开发(基础篇)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7065294.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十四)--S ...

  7. SpringMvc注解开发

    1.四大注解的定义 (1)Controller注解:该注解使用在一个类上面,参数为value,值为访问该controller的名称,默认为空,如果为空 则值为该controller类名的首字母小写的值 ...

  8. springMVC注解初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  9. SpringMVC-Mybatis整合和注解开发

    SpringMVC-Mybatis整合和注解开发SpringMVC-Mybatis整合整合的思路在mybatis和spring整合的基础上 添加springmvc.spring要管理springmvc ...

随机推荐

  1. ABAP的匹配

    ABAP的匹配 通配符 字符串操作中的通配符 *:多位字符的通配符 +:一位字符的通配符 #:字符操作中的转义符 REPORT ztest_placeholder. DATA:l_name(8) TY ...

  2. odoo TransientModels must have log_access turned on

  3. CentOS7升级默认内核

    安装内核升级镜像源 rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm Yum安装内核 yum --e ...

  4. SQL 2008发布与订阅

    网的教程很多,大都是不能成功,只有这一篇是成功的! https://www.cnblogs.com/DBArtist/p/5803271.html

  5. Matting任务里的Gradient与Connectivity指标

    Matting任务里的Gradient与Connectivity指标 主要背景 Matting任务就是把α(不透明度, 也就是像素属于前景的概率).F(前景色)和B(背景色)三个变量给解出来. C为图 ...

  6. 1004: [HNOI2008]Cards - burnside + DP

    Description 小春现在很清闲, 面对书桌上的 \(N\) 张牌, 他决定给每张染色, 目前小春只有 \(3\) 种颜色: 红色, 蓝色, 绿色. 他询问 Sun 有 多少种染色方案, Sun ...

  7. Vue 中使用 viewerjs进行本地上传预览图片

    https://www.cnblogs.com/shenjp/p/9754171.html 如果图片路径是 接口的返回信息的话,将路径存储在数组中,在this.$nextTick中实例化Viewer: ...

  8. JavaScript初见

    警告alert() 确认confirm() 提问prompt() 空格 JavaScript-打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: ...

  9. 内核中hash表(以net_device为例)

    下边函数实现将新的 net_device 设备插入到内核链表中 /* * Device list insertion */ static void list_netdevice(struct net_ ...

  10. docker构建镜像

    Docker 提供了两种构建镜像的方法: docker commit 命令Dockerfile 构建文件 示例: Dockerfile FROM golang:1.7.5 #基础镜像 RUN apt- ...