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>