[Spring MVC] - 地址路由使用(一)
常用的一些Spring MVC的路由写法以及参数传递方式。
参考引用:
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html
这是一些测试例子:
package com.my.controller; import java.io.PrintWriter;
import java.util.Date; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value="/default")
public class TestController { /**
* 使用ModelAndView
* @return
*/
@RequestMapping("/")
public ModelAndView index() {
ModelAndView mv = new ModelAndView();
mv.addObject("message", "This is index!");
mv.setViewName("index");
return mv;
} /**
* 使用Model
* @param model
* @return
*/
@RequestMapping("/index2")
public String index2(Model model) {
model.addAttribute("message", "This is index2!");
return "index2";
} /**
* 直接返回内容
* @return
*/
@RequestMapping(value="/index3", method=RequestMethod.GET)
@ResponseBody
public String index3() {
return "This is index3!";
} /**
* 直接返回对应的index4的view
*/
@RequestMapping(value="/index4", method=RequestMethod.GET)
public void index4() {
} /**
* 直接返回index5的view,注意方法后缀:Handler
* 比较与index4的区别
*/
@RequestMapping(value="/index5", method=RequestMethod.GET)
public void index5Handler() {
} /**
* 传参方式调用
* @param message
* @return
*/
@RequestMapping(value="/index6/{message}", method=RequestMethod.GET)
public ModelAndView index6(@PathVariable String message) {
ModelAndView view = new ModelAndView("index6");
view.addObject("message", message);
return view;
} /**
* 传参方式调用,使用Model
* 注意方法内的Model是直接addAttribute,没有key
* 区别index6
* @param message
* @param model
* @return
*/
@RequestMapping(value="/index7/{message}", method=RequestMethod.GET)
public String index7(@PathVariable String message, Model model) {
model.addAttribute(message);
return "index6";
} /**
* 传参调用
* 指定参数名称
* @param userName
* @param password
* @return
*/
@RequestMapping(value="/index8/{username}/{password}")
public ModelAndView index8(@PathVariable("username") String userName, @PathVariable("password") String password) {
ModelAndView view = new ModelAndView("index8");
view.addObject("userName", userName);
view.addObject("password", password);
return view;
} /**
* 使用request/response取得参数
* @param request
* @param response
* @return
*/
@RequestMapping(value="/index9")
public String index9(HttpServletRequest request, HttpServletResponse response) {
String message = request.getParameter("message");
request.setAttribute("message", message);
return "index9";
} /**
* 使用@RequestParam方式取得参数
* @param message
* @return
*/
@RequestMapping(value="/index10")
public ModelAndView index10(@RequestParam("message") String message) {
ModelAndView view = new ModelAndView("index10");
view.addObject("message", message);
return view;
} /**
* 默认传参方式
* @param message
* @param model
* @return
*/
@RequestMapping(value="/index11")
public String index11(String message, Model model) {
model.addAttribute("message", message);
return "index10";
} /**
* 传入PrintWriteer,也可以传入@CookieValue
* @param out
* @param message
* @param model
* @return
*/
@RequestMapping(value="/index12")
public String index12(PrintWriter out, String message, Model model) {
out.println(message);
model.addAttribute("message", message);
return "index10";
} /**
* 日期参数
* @param day
* @return
*/
@RequestMapping(value="/index13")
public ModelAndView index13(@RequestParam("day") @DateTimeFormat(pattern="yyyy-MM-dd") Date day) {
ModelAndView view = new ModelAndView("index10");
view.addObject("message", day.toString());
return view;
} /**
* 默认action
* @param message
* @return
*/
@RequestMapping
public ModelAndView index14(String message) {
ModelAndView view = new ModelAndView("index");
view.addObject("message", message);
return view;
} /**
* 限制访问的地址,必需有参数p=1才能触发
* @param message
* @param model
* @return
*/
@RequestMapping(value="/index15/{message}", params="p=1", method=RequestMethod.GET)
public String index15(@PathVariable("message") String message, Model model) {
model.addAttribute("message", message);
return "index";
} }
[Spring MVC] - 地址路由使用(一)的更多相关文章
- Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式
ylbtech-Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式 1.返回顶部 1. 常用的一些Spring MVC的路由写法以及参数传递方式. 这是 ...
- Spring mvc创建的web项目,如何获知其web的项目名称,访问具体的链接地址?
Spring mvc创建的web项目,如何获知其web的项目名称,访问具体的链接地址? 访问URL: http://localhost:8090/firstapp/login 在eclipse集成的 ...
- Spring mvc源码url路由-我们到底能走多远系列(38)
我们到底能走多远系列38 扯淡: 马航的事,挺震惊的.还是多多珍惜身边的人吧. 主题: Spring mvc 作为表现层的框架,整个流程是比较好理解的,毕竟我们做web开发的,最早也经常接触的就是一个 ...
- 使用Spring mvc接收整个url地址及参数时注意事项
使用Spring mvc接收整个url地址及参数时注意事项:url= http://baidu?oid=9525c1f2b2cd45019b30a37bead6ebbb&td=2015-08- ...
- 基于Spring Boot构建的Spring MVC快速入门
原文地址:http://tianmaying.com/tutorial/spring-mvc-quickstart 环境准备 一个称手的文本编辑器(例如Vim.Emacs.Sublime Text)或 ...
- Spring MVC URL传参
Spring MVC 学习 之 - URL参数传递 在学习 Spring Mvc 过程中,有必要来先了解几个关键参数: @Controller: 在类上注解,则此类将编程一个控制器,在项目启 ...
- Spring MVC中页面向后台传值的几种方式
在学习 Spring Mvc 过程中,有必要来先了解几个关键参数: @Controller: 在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对 ...
- 改造继续之eclipse集成tomcat开发spring mvc项目配置一览
在上一篇的环境配置中,你还只能基于maven开发一个javase的项目,本篇来看如果开发一个web项目,所以还得配置一下tomcat和spring mvc. 一:Tomcat安装 在.net web开 ...
- 朱晔和你聊Spring系列S1E4:灵活但不算好用的Spring MVC
阅读PDF版本 本文会以一些例子来展现Spring MVC的常见功能和一些扩展点,然后我们来讨论一下Spring MVC好用不好用. 使用SpringBoot快速开始 基于之前的parent模块,我们 ...
随机推荐
- swig之于c++
[namespace] namespace nsTest1 { int nsAdd(int a, int b) { return a + b; } } namespace nsTest2 { int ...
- Gson手动序列化POJO(工具类)
gson2.7版本 只是简单的工具类(练习所用): package pojo; import javax.xml.bind.annotation.XmlSeeAlso; import com.goog ...
- selenium eclipse环境搭建
1.python 3.5下载及安装 2.setuptools 与pip 下载地址是:http://pypi.Python.org/pypi/setuptools http://pypi.Python. ...
- 用python的BeautifulSoup分析html 【转】
原地址:http://www.cnblogs.com/twinsclover/archive/2012/04/26/2471704.html 序言 之前用python爬取网页的时候,一直用的是rege ...
- [转]Java 8:不要再用循环了
以下内容为转载,没有在jdk8中测试,具体业务场景是否存在BUG或使用需要注意的地方有待测试. ------------------分割线---------------------- 正如我之前所写的 ...
- input框限制只能输入正整数,逻辑与和或运算
推荐下自己刚写的项目,请大家指正:童话之翼 有时需要限制文本框输入内容的类型,本节分享下正则表达式限制文本框只能输入数字.小数点.英文字母.汉字等代码. 例如,输入大于0的正整数 代码如下: < ...
- Android 命令管理项目
今天介绍一下Android怎么用命令管理项目,用命令管理Android项目需要用到Android命令,首先介绍一下用Android命令创建新项目,打开命令提示窗口,导航到Android SDK 的to ...
- 使用HTML5+CSS3制作圆角内发光按钮----示例
<!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Windows Server 2008(R2)配置apache+php+mysql环境问题事项
服务器环境:Windows 2008 R2 64位.apache,mysql,php都是32位. 1. 80端口的外网访问问题 表现:80端口本地可以访问,外网不能访问,换了8080端口也是一样,检查 ...
- PC缺少一个或多个网络协议 qq可登录(win10)
打开适配器连接 1打开网络适配器 2卸载microsoft 3 网络客户端 4重启