springboot页面跳转

package com.example.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    //@Controller跳转页面
    @RequestMapping("/hello")
    public String hello() {
        System.out.println(1);
        return "demo.html";
    }
    
    //ModelAndView仅跳转页面
    @RequestMapping("/path1")
    public ModelAndView test1() {
        return new ModelAndView("h1.html");
    }
    
    //跳转并传递{key:value}
    @RequestMapping("/path2")
    public ModelAndView test2() {
        
        return new ModelAndView("h2.html").addObject("hello", "world");
    }
    
    
    //跳转带map形式
    @RequestMapping("/path3")
    public ModelAndView test3() {
        Map<String,Object> map=new HashMap<String,Object>();
    
        map.put("foo", 1.5f);
        map.put("bar", "hello");
        
        return new ModelAndView("h4.html").addAllObjects(map);
    }
    
    //跳转带list套map形式
        @RequestMapping("/path4")
        public ModelAndView test4() {
            Map<String,Object> map=new HashMap<String,Object>();
            Map<String,Object> map2=new HashMap<String,Object>();
            List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
            
            map.put("foo", 1.5f);
            map.put("bar", "hello");
            
            map2.put("foo", 123);
            map2.put("bar", "world");
            list.add(map);
            list.add(map2);
            
            return new ModelAndView("h3.html").addObject("list",list);
        }
    
}
本例使用@Controller进行页面跳转,响应请求和JSON的形式见此系列其它文档

页面用Thymeleaf

h1.html (略)

h2.html

<body>
<b th:text="${hello}"></b>
</body>

h3.html

<body>
<b th:text="${foo}" /><br>
<b th:text="${bar}" />
</body>

h4.html

<body>
<th:block th:each="row : ${list}" >
<b th:text="${row.foo}" />
<b th:text="${row.bar}" /><br>
</th:block>

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值