博客
关于我
SpringMVC---使用
阅读量:304 次
发布时间:2019-03-04

本文共 2023 字,大约阅读时间需要 6 分钟。

关键词

@RequestMapping

@RequestMapping("/xxxx")

功能:在方法,类中添加该注解,相当于:浏览器发送请求xxxx。会访问该类中的方法。比如:

public class Test{   	@RequestMapping("/one")    public String one(){   		return "one";    }}

返回的one会给视图解析器进行解析拼接,然后跳转到该页面。

测试url:http://localhost:8080/one

@ResponseBody

就像上面的情况:

public class Test{   	@ResponseBody	@RequestMapping("/one")    public String one(){   		return "one";    }}

当我们加入了该注解,就相当于,这个“one”只是作为响应体,而不是映射某一个文件。

实现转发和重定向

关键词:

  • redirect:重定向
  • forward:转发

没有视图解析器的情况

记得把视图解析器的代码注释掉先。

默认不写的情况是转发。

关于请看这篇文章。

@Controller@RequestMapping("/two")public class TwoController {       @RequestMapping("/test1")    public String getTest1(Model model){           //使用重定向        return "redirect:/index.jsp";    }    @RequestMapping("/test2")    public String getTest2(Model model){           //使用重定向        return "forward:/WEB-INF/jsp/test.jsp";    }    @RequestMapping("/test3")    public String getTest3(Model model){           //使用重定向        return "/WEB-INF/jsp/test.jsp";    }}

有视图解析器的情况

使用重定向和转发的情况一样,但是要注意的是,有视图解析器的话,无论是重定向还是转发,都会对字符串进行拼接

数据的处理

传递参数是一个对象:

User.java:

public class User {       private String name;    private int age;	....}
@Controller@RequestMapping("/two")public class TwoController {           @RequestMapping("/test1")    public String getTest1(User user){                   System.out.println(user);        return "test";    }}

为了将前端传递的数据都封装成一个user对象,字段名必须要根据user的属性进行匹配,不然为空。

访问地址:http://localhost:8080/two/test1?name=tom&age=5

提交域名名称与处理方法参数不一致

关键词:@RequestParam(“xxxx”)

括号内的名称为域名提交的名称。

@Controller@RequestMapping("/two")public class TwoController {       @RequestMapping("/test1")    public String getTest1(@RequestParam("username") String name){           System.out.println(name);        return "test";    }}

乱码问题:

在web.xml配置Spring提供的乱码过滤器:

CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
/*

转载地址:http://crlq.baihongyu.com/

你可能感兴趣的文章
2021年重氮化工艺考试题库及重氮化工艺考试报名
查看>>
2021年车工(高级)考试总结及车工(高级)试题及答案
查看>>
2021年压力焊证考试及压力焊实操考试视频
查看>>
2021年低压电工考试及低压电工考试申请表
查看>>
2021年低压电工考试及低压电工考试申请表
查看>>
2021年A特种设备相关管理(电梯)考试APP及A特种设备相关管理(电梯)复审考试
查看>>
2021年N1叉车司机考试题及N1叉车司机复审模拟考试
查看>>
2021年T电梯修理考试技巧及T电梯修理模拟考试软件
查看>>
CodeBlocks开发wxWidgets环境配置详细
查看>>
天涯人脉通讯录 - 设计草图
查看>>
wxWidgets 最新版2.8.11,终于放出来了
查看>>
python学习09:暂停一秒后再输出
查看>>
6、ShardingSphere 之 读写分离
查看>>
C++ STL
查看>>
解方程
查看>>
Netty 粘包 拆包 | 史上最全解读
查看>>
【调剂】其它计算机/软件调剂信息 20.4.21
查看>>
【调剂】华侨大学媒体分析与数据挖掘小组招收学硕调剂生
查看>>
JavaScript学习手册(45)
查看>>
eclipse中server location灰色解决
查看>>