HelloController.java
package com.demo.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/index")
public class HelloController {
// method=RequestMethod.POST指定当前方法只能有POST访问
@RequestMapping(value="/hello.do",method=RequestMethod.POST)
public ModelAndView sayHello(){
Map<String,Object> model = new HashMap<>();
model.put("hello", "你好");
model.put("world", "世界");
ModelAndView mav= new ModelAndView("hello", model);
return mav;
}
@RequestMapping(value="/one")
public String methodOne(Model model){
model.addAttribute("world","我的");
return "success";
}
@RequestMapping(value="/two.do")
public String methodTwo(Model model){
model.addAttribute("word", "我的");
// 重定向不走视图解析器
// /表示绝对路径,如果不加/,则表示相对路径
return "redirect:/success.jsp";
}
@RequestMapping(value="/three.do")
public ModelAndView methodThree(){
ModelAndView mav = new ModelAndView();
mav.setViewName("redirect:/success.jsp");
System.out.println("地址:"+this);
return mav;
}
@RequestMapping(value="/four.do")
public String methodFour(HttpServletRequest req){
String nameStr = req.getParameter("name");
System.out.println("nameStr:"+nameStr);
return "success";
//http://localhost:8080/ssy/index/four.do?name=zhangsanasdfg
}
@RequestMapping(value="/five.do")
public void methodFive(HttpServletResponse resp) throws IOException{
resp.setCharacterEncoding("utf-8");
PrintWriter pw = resp.getWriter();
/* pw.write("<%@ page contentType='text/html; charset='UTF-8' pageEncoding='UTF-8'%>");
pw.write("<html>");
pw.write("<body>");
pw.write("你好,我是write写的字符.");
pw.write("</body>");
pw.write("</html>");*/
pw.write("hello word!");
//http://localhost:8080/ssy/index/five.do
}
@RequestMapping("/six")
public String methodSix(HttpServletResponse resp, HttpServletRequest req,
HttpSession session) {
String jsessionId = session.getId();
System.out.println("jsessionId:" + jsessionId);
return "success";
}
}
index.html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- <form action="index/hello.do" method="post"> -->
<form action="index/five.do" method="post">
<!-- <form action="index/two.do" method="post">
<form action="index/three.do" method="post">
<form action="index/four.do" method="post">
<form action="index/five.do" method="post">
<form action="index/six.do" method="post"> -->
<input type="submit" value="提交"/>
</form>
</body>
</html>
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${hello }
${world }
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${world }
</body>
</html>
eclipse导出
项目源码上传资源等待审核中