当前位置:网站首页>SpingMVC请求和响应
SpingMVC请求和响应
2022-06-29 16:53:00 【二哈喇子!】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
提示:以下是本篇文章正文内容,下面案例可供参考
web.xml中配置
<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:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
页面提交数据方法
1、直接通过url请求地址通过 “?” 拼接请求参数的方式。
2、通过页面的form表单 submit类型的按钮提交的方式(get和post)。
登录按钮的类型(type属性)必须为“submit”
3、通过JavaScript (JQuery) ,Ajax异步请求提交数据。
(数据来源可以是通过form表单,或通过其他方式获得的数据)
系统功能编码流程

控制层接收数据的几种方法:
1、通过简单类型参数绑定的方式。
例如:控制器方法名(Stringname)。还可以借助于@RequestParam注解进行间接绑定。
2、通过pojo类,即封装数据类型的方式进行请求参数绑定。
例如:控制器方法名(User user) 。
3、通过默认数据类型的方式进行绑定,
例如:控制器方法名(HttpServletRequest req)
4、Restful编程风格(把请求参数放到请求路径中)
注解
@Controller注解
@Controller 注解类型用于指示Spring类的实例是一个控制器。使用时不需要再实现Controller 接口,只需要将 @Controller注解加入到控制器类上,通过Spring的扫描机制找到标注了该注解的控制器即可。
package com.controller;
import org.springframework.stereotype.Controller;
...
@Controller
public class Hello {
...
}
注意:
使用注解方式时,程序的运行需要依赖 Spring的 AOP包
因此需要向lib目录中添加spring-aop-4.3.6.RELEASE.jar,否则程序运行时会报错!
为保证Spring能够找到控制器类,需要在 Spring MVC的配置文件中添加相应的扫描配置信息:
<!-- 配置扫描器 -->
<context:component-scan base-package="springmvc.controller"/>
@RequestMapping注解
Spring通过 @Controller注解找到相应的控制器类后,还需要知道控制器内部对每一个请求是如何处理的,这就需要使用 @RequestMapping注解,它用于映射一个请求或一个方法。使用时,可以标注在一个方法或一个类上。
1.标注在方法上:作为请求处理方法在程序接收到对应的URL请求时被调用:
package com.itheima.sontroller;
import org.springframework.stereotype.Controller;
import org.springframework.wsb.bind.annotation.RequestMapping;
...
@Controller
@RequestMapping(value="/hello")
public class FirstController{
@RequestMapping(value="/firstController")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletR.esponse response){
...
return mav;
}
}
由于在类上添加了@RequestMapping注解,并且其value属性值为 “/hello”,
所以上面代码方法的请求路径将变为: http://localhost:8080/chapter12/ hello/firstController
@RequestMapping注解的属性

组合注解
@GetMapping:匹配 GET方式的请求
@PostMapping:匹配 POST方式的请求
@PutMapping:匹配 PUT方式的请求
@DeleteMapping:匹配 DELETE方式的请求
@PatchMapping:匹配 PATCH方式的请求
举例:
@RequestMapping(value = "/login.action", method = RequestMethod.POST)
//这两个注解的作用一样
@PostMapping("/login.action")
@RequestMapping(value = "/login.action", method = RequestMethod.GET)
//这两个注解的作用一样
@GetMapping("/login.action")
在控制层对用户响应页面或数据的方式
对用户响应页面或数据的方式,即请求处理方式的返回类型。常见类型有三种:
ModelAndView
String:使用String来表示要返回的界面,使用Model返回界面需要的值。
void:用于异步请求,只返回数据,不跳转页面。
使用String表示要返回的界面
Action的方法返回值类型为String,在方法中返回表示输出界面的字符串,与ModelAndView中的第一个参数相同
@RequestMapping("studentAddView.do")
public String addView(){
return "studentAdd";
}
如果要实现上面这段代码,就要实现视图解析器
把下面这段代码放在配置文件中(.xml)
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
边栏推荐
- 基于C语言开发实现的一个用户级线程库
- Flutter technology and Practice (1)
- [day 28] given a string s, please judge whether it is a palindrome string | palindrome judgment
- What memory consuming data is stored in MySQL CDC jobmanager?
- MySQL cdc jobmanager 中存了哪些比较耗内存的数据呢?
- Profil de risque de monnaie stable: l'usdt et l'USDC sont - ils sûrs?
- An error is reported in the Flink SQL rownumber. Who has met him? How to solve it?
- After studying this series of notes about software testing, it is a "bonus" to enter the factory
- 6.26CF模拟赛D:黑白条题题解
- c# 国内外ORM 框架 dapper efcore sqlsugar freesql hisql sqlserver数据常规插入测试性能对比
猜你喜欢

能够1年涨薪2次的软件测试工程师,他到底强在哪里?

最高81.98%!超百所“双一流”高校本科深造率公布

可转债策略之---(摊饼玩法,溢价玩法,强赎玩法,下修玩法,双低玩法)

ICML 2022 | 基于解耦梯度优化的可迁移模仿学习方法

【南京大学】考研初试复试资料分享

After reading the complete code

In order to prevent being rectified after 00, a company requires employees not to sue the company

基于汇编实现的流载体的LSB隐藏项目

使用kalibr標定工具進行單目相機和雙目相機的標定

全面剖析Seata 分布式事务 AT 与XA
随机推荐
『计组』CPU 如何区分指令和数据
垃圾收集器
In depth analysis of Monai (I) data and transforms
Top the list for 10 consecutive years? What is the "most common" programming language for programmers?
C# Winfrom Chart图表控件 柱状图、折线图
About xampp unable to start MySQL database
Graduates are confused and middle-aged people are anxious. How can the career path become wider and wider?
卷妹带你学数据库---5天冲刺Day1
Comment configurer logback? 30 minutes pour apprendre à coder et à frapper tard.
微信小程序开发储备知识
机器学习8-人工神经网络
XAMPP Apache安装时问题总结
Paper notes: e (n) equivariant graph neural networks
如何配置 logback?30分钟让你彻底学会代码熬夜敲
6.25atcoderabc257e - addition and multiplication 2
同样是做测试,为什么别人年薪30W+?
How to distinguish between instructions and data in the "group counting" CPU
【南京大学】考研初试复试资料分享
Leetcode 984. 不含 AAA 或 BBB 的字符串(网友思路)
After 3 years of testing experience, do you know the state transition method for use case design?