美文网首页
四、ControllerToView和视图解析器

四、ControllerToView和视图解析器

作者: lifeline张 | 来源:发表于2019-01-24 17:23 被阅读0次

一、如何将模型数据传递给视图

1、1第一种方式

image.png

示例代码:

package cn.smbms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sun.istack.internal.logging.Logger;

@Controller
public class IndexController {

    private Logger logger = Logger.getLogger(IndexController.class);
    
    @RequestMapping("/index")
    public String index() {
        logger.info("hello, springmvc");
        return "index";
    }
    
    @RequestMapping(value="/welcome", method=RequestMethod.GET, params="username")
    public String welcome(@RequestParam String username) {
        logger.info("hello, springmvc! username:" + username);
        return "index";
    }
    
    @RequestMapping("/index1")
    public ModelAndView index(String username) {
        ModelAndView mView = new ModelAndView();
        mView.addObject("username", username);
        mView.setViewName("index");
        logger.info("hello, springmvc");
        return mView;
    }
    
    
}

jsp:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <h1>welcome.spring MVC!</h1>
    <h1>username-->  ${username }</h1>
  </body>
</html>

运行结果正常。

1.2第二种方式

image.png

示例代码:

package cn.smbms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sun.istack.internal.logging.Logger;

@Controller
public class IndexController {

    private Logger logger = Logger.getLogger(IndexController.class);
    
    @RequestMapping("/index")
    public String index() {
        logger.info("hello, springmvc");
        return "index";
    }
    
    @RequestMapping(value="/welcome", method=RequestMethod.GET, params="username")
    public String welcome(@RequestParam String username) {
        logger.info("hello, springmvc! username:" + username);
        return "index";
    }
    
    @RequestMapping("/index1")
    public ModelAndView index(String username) {
        ModelAndView mView = new ModelAndView();
        mView.addObject("username", username);
        mView.setViewName("index");
        logger.info("hello, springmvc");
        return mView;
    }
    
    @RequestMapping("/index2")
    public String index2(String username, Model model) {
        model.addAttribute("username", username);
        return "index";
    }
}

如果不指定key的话:

    @RequestMapping("/index2")
    public String index2(String username, Model model) {
        model.addAttribute(username);
        return "index";
    }

则在前台获取数据的时候填入的key是该数据的数据类型:

  <body>
    <h1>welcome.spring MVC!</h1>
    <h1>username-->  ${string }</h1>
  </body>

如果放进去的是user类型的数据:

@RequestMapping("/index2")
    public String index2(String username, Model model) {
        model.addAttribute(username);
        User user = new User();
        user.setUserName(username);
        model.addAttribute(user);
        return "index";
    }

前台代码如下:

<body>
    <h1>welcome.spring MVC!</h1>
    <h1>username-->  ${user.userName }</h1>
  </body>

1.3使用map入参

Model其实就是一个Map的数据结构,那么是否可以使用Map入参呢?
是可以的。
示例代码:

@RequestMapping("/index3")
    public String index3(String username, Map<String, Object>model) {
        model.put("username", username);
        return "index";
    }

前台:

<body>
    <h1>welcome.spring MVC!</h1>
    <h1>username-->  ${username }</h1>
  </body>

注:作为SpringMVC的标准用法,推荐使用Model。

二、视图解析器——ViewResolver

将逻辑视图名与JSP等视图技术进行匹配。


image.png

三、小结

image.png

相关文章

网友评论

      本文标题:四、ControllerToView和视图解析器

      本文链接:https://www.haomeiwen.com/subject/zkdthqtx.html