java—在SpringMVC处理程序方法中使用httpservletresponse对象

uz75evzq  于 2021-07-15  发布在  Java
关注(0)|答案(1)|浏览(342)

我对springmvc和servlet还不熟悉。我试图在控制器类searchcontroller中运行home()方法,但输出仅来自home.jsp文件。声明:

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

在浏览器的结果中不包括。
有什么方法可以在浏览器中用home.jsp文件打印out object语句吗?

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>

这是浏览器上的输出:

bihw5rsg

bihw5rsg1#

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>

如果您想通过controller类为home.jsp添加一些内容,您可以如下更改您的controller类。

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>

为了进一步的解释,请通过这个链接了解如何从servlet发送消息并在jsp中显示

相关问题