SpringMVC使用的几个要点
1.使用 @RequestParam("username") 来对应参数名的时候,这个参数必须要传入,否则会报错。没加@RequestParam则可传可不传
@RequestMapping("/index")
public String index(@RequestParam("username") String username, String password) {
System.out.println(username);
System.out.println(password);
return "test/index";
}
2.向页面传值,可以用Map也可以用Model,通常都用Model
@RequestMapping("/index2")
public String index2(String username, Map<String, Object> context) {
System.out.println(username);
context.put("username", username);
return "test/index";
}
@RequestMapping("/index3")
public String index3(String username, Model model) {
// System.out.println("id:" + id);
System.out.println(username);
model.addAttribute("username", "username");
// 只有一个参数,默认key就是小写的对象名称
model.addAttribute(new Article());
return "test/index";
}
页面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>test ${username }
${article.id }
</body>
</html>
3. method = RequestMethod.GET 代表get方式访问
@RequestMapping(value = "/index4", method = RequestMethod.GET)
public String index4(String username, Model model) {
// System.out.println("id:" + id);
System.out.println(username);
model.addAttribute("username", "username");
// 只有一个参数,默认key就是小写的对象名称
model.addAttribute(new Article());
return "test/index";
}
4. 通过url传值用 @PathVariable @RequestMapping value可以直接支持多级目录的路径,不像asp.net mvc需要在global.asax中设置路由
// www.url.com/test/p1 读取url的地址作为参数
@RequestMapping(value = "/{username}", method = RequestMethod.GET)
public String show(@PathVariable String username) {
System.out.println(username);
return "test/index";
} // www.url.com/test/p1 读取url的地址作为参数
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
public String show2(@PathVariable String id) {
System.out.println("show2 " + id);
return "test/index";
} @RequestMapping(value = "/update/detail/{id}", method = RequestMethod.GET)
public String show3(@PathVariable String id) {
System.out.println("show3 " + id);
return "test/index";
}
5. (1) 可以通过 bean-validator.jar 对springMVC 做服务端校验,校验时@Validated Menu menu, BindingResult br 参数必须写在一起
@NotEmpty(message="菜单名单不能为空")
public String getMenuname() {
return menuname;
}
(2) springMVC上传文件需要使用commons-io-2.2.jar、commons-fileupload-1.3.1.jar两个jar包
配置文件中需要加入bean代码
<!-- 设置multipartResolver才能完成文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 限制文件最大值为5M -->
<property name="maxUploadSize" value="5000000"></property>
</bean>
@RequestMapping(value = "/menuadd", method = RequestMethod.POST)
public String menuadd(@Validated Menu menu, BindingResult br,@RequestParam("attachs") MultipartFile[] attachs, HttpServletRequest req) throws IOException {// 紧跟Validated写验证结果类
System.out.println(menu.getMenuid() + " " + menu.getMenuname());
if (br.hasErrors()) {
System.out.println("验证不通过");
return "redirect:/test/menuadd";// 客户端跳转
}
for (MultipartFile attach : attachs) {
if (attach.isEmpty())
continue; System.out.println(attach.getName() + "," + attach.getOriginalFilename() + "," + attach.getContentType());
String path = req.getSession().getServletContext().getRealPath("/WEB-INF/resources/upload");
System.out.println(path);
File file = new File(path + "/" + attach.getOriginalFilename());
FileUtils.copyInputStreamToFile(attach.getInputStream(), file);
} return "redirect:/test/index2";// 客户端跳转
}
页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@taglib prefix="sf" uri="http://www.springframework.org/tags/form"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> <sf:form method="post" modelAttribute="menu" enctype="multipart/form-data">
menuid:<input type="text" name="menuid"><br/>
menuname:<input type="text" name="menuname"><br>
<sf:errors path="menuname"></sf:errors><br/>
file1:<input type="file" name="attachs"><br/>
file2:<input type="file" name="attachs"><br/>
file3:<input type="file" name="attachs"><br/>
<input type="submit" value="提交">
</sf:form> </body>
</html>
6.跳转页面可以使用 return "redirect:/test/index2"; // 客户端跳转
7. 返回json数据 加@ResponseBody 并且直接return对象
params="jj" 代表如果传了jj参数就可以映射到这个方法执行,可以根据params参数执行不同的方法
@RequestMapping(value = "/update/detail/{id}", method = RequestMethod.GET,params="jj")
@ResponseBody
public Menu show4(@PathVariable String id) {
System.out.println("show4 " + id);
Menu menu = new Menu(12l, "menuName");
return menu;
}
8. produces解决返回中文到客户端乱码的问题
/**
* 启用活动
*/
@RequestMapping(value = "use/{id}", produces = "text/html;charset=UTF-8")
@ResponseBody
public String use(@PathVariable("id") long id) {
InfoWeixinGoldEggActivity info = service.queryById(id);
if (info.getStatus()) {
return "活动已经是启用状态";
}
info.setStatus(true);
service.updateStatus(info);
return "success";
}
SpringMVC使用的几个要点的更多相关文章
- SpringMVC基本使用
springMVC是一个MVC框架,他控制着请求相应的整个流程,从请求一进入到应用服务器到相应离开,都离不开mvc框架 请求在应用服务器中 先说说请求相应在应用服务器的整个过程 DisptacherS ...
- Java Web编程技术学习要点及方向
学习编程技术要点及方向亮点: 传统学习编程技术落后,应跟著潮流,要对业务聚焦处理.要Jar, 不要War:以小为主,以简为宝,集堆而成.去繁取简 Spring Boot,明日之春(future of ...
- SpringMVC源码剖析(四)- DispatcherServlet请求转发的实现
SpringMVC完成初始化流程之后,就进入Servlet标准生命周期的第二个阶段,即“service”阶段.在“service”阶段中,每一次Http请求到来,容器都会启动一个请求线程,通过serv ...
- springmvc学习笔记--ueditor和springmvc的集成
前言: 在web开发中, 富文本的编辑器真心很重要. 有电商店铺的打理, 新闻稿/博客文章/论坛帖子的编辑等等, 这种所见即所的编辑方式, 大大方便了非技术人员从事互利网相关的工作. 因为手头有个小项 ...
- 【maven + hibernate(注解) +spring +springMVC】 使用maven搭建项目
研究,百度,查资料+好友帮助,使用MyEcplise2015工具,通过maven搭建hibernate+springMVC+spring的项目,数据库采用MySql5.5 不过使用的版本会在项目搭建过 ...
- idea使用maven搭建springmvc
最近学着搭建springmvc,写此博客记录一下 idea版本:2016.3.1maven: apache-maven-3.3.9tomcat:apache-tomcat-8.5.8 1.New Pr ...
- SpringMVC深入探究(1)——DispatcherServlet与初始化主线
在上一篇文章中,我们给出了构成SpringMVC应用程序的三要素以及三要素的设计过程.让我们来归纳一下整个设计过程中的一些要点: SpringMVC将Http处理流程抽象为一个又一个处理单元 Spri ...
- 浅谈SpringMVC(一)
一.SpringMVC引言 Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 MV ...
- 蓝缘管理系统第二个版本号开源了。springMVC+springSecurity3.x+Mybaits3.x 系统
蓝缘管理系统第二个版本号开源了 继于 http://blog.csdn.net/mmm333zzz/article/details/16863543 版本号一.版本号二 对springMVC+spri ...
随机推荐
- LVM逻辑卷基本概念及LVM的工作原理
这篇随笔将详细讲解Linux磁盘管理机制中的LVM逻辑卷的基本概念以及LVM的工作原理!!! 一.传统的磁盘管理 其实在Linux操作系统中,我们的磁盘管理机制和windows上的差不多,绝大多数都是 ...
- MINIX3
这个系列minix3是好早看的源码 现在都忘记的差不多了 觉得就此扔掉可惜了 今天把他全部放在博客上 1 是想和大家一起讨论下 2 是没事看看 能够加强对一个稳定性系统的理解 加厚
- winform 使用 ReportViewer做报表
之前用过的水晶报表觉得有些麻烦,因此尝试了使用微软自带的报表. 第一种方法是 在winform界面上放置ReportViewer界面,相关的代码如下: public DataTable dt; pri ...
- linux 服务的操作
启动和停止服务service 命令用于启动及停止某个服务,例如:service camsd stop 停止 camsd 服务service oracled start 启动 oracled ...
- Localization要从第一天开始计划
最近E3,微软说可以在任何region玩任何语言的游戏了.换一个语言么,听起来没有那么复杂,其实操作起来还得是从软件工程初期就好好计划. Windows在很长一段时间,你安装完了,就不能换语言了.大学 ...
- IIS 发布 异常信息 AspNetInitClrHostFailureModule 的解决办法
昨天在一个客户那里使用Server 2008服务器配置IIS,都配置好之后竟然出现了错误信息,以前没有遇到过 "AspNetInitClrHostFailureModule",于是 ...
- Linux虚拟机安装(CentOS 6.5,图文详解,需要自查)
Linux虚拟机的安装(图文详解) 下篇会接续Hadoop集群安装(以此为基础) 一.安装准备 VMWorkstation.linux系统镜像(以下以CentOS6.5为例) 二.安装过程详解 关闭防 ...
- solr+mongo-connector+mongdb+tomcat集成
话题:solr安装 一.下载solr 本例采用4.10.3版本. Solr所有版本下载地址:http://archive.apache.org/dist/lucene/solr/ 下载完成后,解压的目 ...
- TYVJ 1117 BFS
无限WA..参考了一下题解和同学写的....... 可以在bfs的基础上改一下.. 读入的时候平地权值是2 草地是0 bfs的时候如果搜到的是平地,那么直接加入,如果搜到的是草地,那么记录是草地. 从 ...
- Linux 下编译安装MySQL
最近在研究Mysql,当然先要把它安装在机器上才行呀.记录下操作,加深记忆,也供以后参考. 准备工作: Linux版本:Redhat Linux 6.4 Mysql版本(安装包):mysql-5.6. ...