zoukankan      html  css  js  c++  java
  • 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>
     
  • 相关阅读:
    模块添加页代码
    模块登录页代码
    列表登录页代码
    不在让你为你写代码头疼的链接页代码
    最新最全产品删除页代码
    网站的产品页后台代码
    Windows修改账户名称和任务管理器中服务对应的用户名称
    nginx处理HTTP header问题
    Maven报错:Missing artifact jdk.tools:jdk.tools:jar:1.6
    linux上传、下载文件rz、sz命令
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11945667.html
Copyright © 2011-2022 走看看