springMVC return value acquisition mode in response to the

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" ;
}

/ **
* Test Method testVoid
* @param * @return * / @RequestMapping ( path = "/ testVoid") public void testVoid (the HttpServletRequest Request , the HttpServletResponse Response) throws Exception { the System. Out.println ( " testString executed successfully ") ; // request forwarding // request.getRequestDispatcher (" / the WEB-INF / Pages / success.jsp ") forward (request, Response);. // redirection (redirection request is not transmitted directly access to the page in the WEB-INF) // Response.sendRedirect (request.getContextPath () + "/ the index.jsp"); // flow directly responds // set Chinese distortion 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>
 

Guess you like

Origin www.cnblogs.com/newcityboy/p/11945667.html