• springMVC中响应的返回值获取方式


    package com.hope.controller;

    import com.hope.domain.User;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    /**
    * @author newcityman
    * @date 2019/11/27 - 20:38
    */
    @Controller("userController")
    @RequestMapping(path ={"/user"} )
    public class UserController {
    /**
    * 返回值是String
    * @param model
    * @return
    */
    @RequestMapping(path = "/testString")
    public String testString(Model model){
    System.out.println("testString执行成功");
    User user = new User();

    user.setUsername("zmy");
    user.setPassword("123");
    user.setAge(12);
    model.addAttribute("user",user);
    return "success";
    }

    /**
    * 测试testVoid方法
    * @param
    * @return
    */

    @RequestMapping(path = "/testVoid")
    public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {
    System.out.println("testString执行成功");
    //请求转发
    // request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
    //重定向(重定向是不能够直接发送请求去访问WEB-INF下的页面的)
    // response.sendRedirect(request.getContextPath()+"/index.jsp");
    //直接流进行响应
    //设置中文乱码
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    response.getWriter().print("hello 张三");
    return;
    }


    }


    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <a href="user/testString">testString</a><br/>

    <a href="user/testVoid">testVoid</a>
    </body>
    </html>


    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <h3>执行成功</h3>

    ${user.username}<br/>
    ${user.age}<br/>
    ${user.password}<br/>
    </body>
    </html>
     
  • 相关阅读:
    Linux-解决putty无法直接使用root用户远程登录linux主机的问题
    Linux-安装jdk
    Linux-安装谷歌浏览器
    Linux-安装ssh服务
    java正则表达式匹配文本中想要的字符串
    日常技术资源收集
    《java编程思想》:散列的原理
    《java编程思想》:字符串
    win10完美去除快捷方式小箭头
    WS_2012
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11945667.html
Copyright © 2020-2023  润新知