在 spring MVC 处理程序方法中使用 HttpServletResponse 对象

Using HttpServletResponse object in spring MVC handler method

我是 Spring MVC 和 Servlet 的新手。我正在尝试在控制器 class SearchController 中使用 运行 home() 方法,但输出仅来自 home.jsp 文件。声明:

out.println("<h1> this is my response block</h1>");

未包含在浏览器的结果中。

有什么办法可以在浏览器中用 home.jsp 文件打印 out 对象 语句吗?

package springmvcsearch;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SearchController {
    @RequestMapping("/home")
    public String home(HttpServletResponse res) throws IOException {

        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        out.println("<h1> this is my response block</h1>");
        
return "home";
    }

}

这是home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
    rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>


</head>
<body>
    This is home view
    
</body>
</html>

这是浏览器上的输出:

out.println() 用于 jsp 文件。所以你可以编辑你的home.jsp如下

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
    rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>


</head>
<body>
    This is home view
      <%
         out.println("<h1> this is my response block</h1>");
      %>
</body>
</html>

如果您想通过控制器 class 为 home.jsp 添加内容,您可以按如下方式更改您的控制器 class。

package springmvcsearch;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SearchController {
    @RequestMapping("/home")
    public String home(HttpServletRequest req,HttpServletResponse res) throws IOException {

        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        String message = "this is my response block";
        req.setAttribute("message", message);
        
         return "home";
    }

}

然后按如下方式更改 jsp 文件。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link href="<c:url value='/resources/css/style.css' />"
    rel="stylesheet">
<script src="<c:url value='/resources/js/sample.css' /> "></script>


</head>
<body>
    This is home view
    <h1>${message}</h1>
</body>
</html>

如需进一步说明,请查看此 link how to message from servlet and display in jsp