SpringMVC入门学习---乱码处理和Restful风格

栏目: Java · 发布时间: 6年前

内容简介:springmvc中提供CharacterEncodingFilter,处理post乱码在web.xml配置过滤器如果是get方式乱码

springmvc中提供CharacterEncodingFilter,处理post乱码

在web.xml配置过滤器

<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-8</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
复制代码

如果是get方式乱码

a)修改tomcat的配置解决

b)自定义乱码解决的过滤器

2.restful风格的url

优点:轻量级,安全,效率高

@RequestMapping("/hello/{name}")
public String hello(@PathVariable String name) {
	System.out.println(name);
	return "index.jsp";
}
复制代码

当我们输入 http://localhost:8080/02springweb_annotation/hello/1.do 的时候,控制台就会输出 1

我们也可以将参数放在hello前面,也可以拥有多个参数。

@RequestMapping("/{name}/{id}/hello")
public String hello(@PathVariable String name,@PathVariable int  id) {
	System.out.println(name);
	System.out.println(id);
	return "index.jsp";
}
复制代码

输入 http://localhost:8080/02springweb_annotation/asd/10/hello.do ,就可以在控制台看到结果了。

3.同一个controller通过参数来到达不同的处理方法--不重要

@Controller
@RequestMapping("/hello2")
public class ControllerMethod {

	@RequestMapping(params="method=add")
	public String add() {
		System.out.println("add");
		return "redirect:/index.jsp";
	}
	
	@RequestMapping(params="method=delete")
	public String delete() {
		System.out.println("delete");
		return "redirect:/index.jsp";
	}
}
复制代码

输入 http://localhost:8080/02springweb_annotation/hello2.do?method=add ,控制台就会输出 add ,代表add方法执行了。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

思考的乐趣

思考的乐趣

顾森 / 人民邮电出版社 / 2012-6 / 45.00元

本书是一个疯狂数学爱好者的数学笔记,面向所有喜爱数学的读者。从2005年7月开始,作者已经写了连续六年的博客,积累下来了大量的数学文章。 部分文章内容被广泛关注,在网络上大量分享转载。 这本书有意挑选了初等的话题,让大大小小的读者都能没有障碍地阅读。文章内容新,让有数学背景的人也会发现很多自己没见过的初等问题。 文章是独立的。一篇文章一个话题,文章与文章之间基本不会做参考,读者可以随意跳着看......一起来看看 《思考的乐趣》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具