package com.jxy.java.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.jxy.java.vo.Kitty;
// 告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("helloController")
public class HelloWorldController {
/**
* 请求路径= 域名 + 项目名 + 类路径 + 方法路径
* 如: http://localhost:8080/jxy/helloController/sayhello
* http://localhost:8080(域名) + /jxy(项目名) + /helloController(类路径) + /sayhello(方法路径)
*/
// 对应请求路劲
@RequestMapping("sayhello")
public void sayHello(HttpServletResponse response){
try {
// 获取输出流
PrintWriter out = response.getWriter();
// 打印hello world!
out.write("Hello World!");
// 发送信息
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 转发jsp页面,方法0:使用原始的request进行转发
*
* @return
*/
@RequestMapping("sayHelloJsp0")
public String sayHelloJsp0(HttpServletRequest request,HttpServletResponse response){
try {
request.setAttribute("hello", "world0");
request.getRequestDispatcher("/WEB-INF/jsp/say_hello.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
};
return "";
}
/**
* 转发jsp页面,方法一:使用返回值为String
*
*/
@RequestMapping("sayHelloJsp1")
public String sayHelloJsp1(HttpServletRequest request,HttpServletResponse response){
request.setAttribute("hello", "world1");
// 返回jsp文件名,jsp路径=配置文件中的prefix + jsp文件名 + 配置文件的suffix
return "say_hello";
}
/**
* 转发jsp页面,方法一:使用返回值为String,同时使用model封装类
* model封装了HttpserlvetRequest、HttpServletResponse
*/
@RequestMapping("sayHelloJsp1_1")
public String sayHelloJsp1_1(HttpServletRequest request,HttpServletResponse response,Model model){
request.setAttribute("kitty", "kitty is a cat.");
model.addAttribute("hello", "world1_1");
// 返回jsp文件名
return "say_hello";
}
/**
* 转发jsp页面,方法一:使用返回值为String,同时使用model封装类
* model替代者modelMap,功能同model
*/
@RequestMapping("sayHelloJsp1_2")
public String sayHelloJsp1_1(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
request.setAttribute("kitty", "kitty is a cat2.");
modelMap.addAttribute("hello", "world1_2");
// 返回jsp文件名
return "say_hello";
}
/**
* 转发jsp页面,方法一:使用返回值为String,同时使用model封装类
* model替代者map,功能同model
* 但是不推荐用,语义不明确
*/
@RequestMapping("sayHelloJsp1_3")
public String sayHelloJsp1_1(HttpServletRequest request,HttpServletResponse response,Map map){
request.setAttribute("kitty", "kitty is a cat3.");
map.put("hello", "world1_4");
// 返回jsp文件名
return "say_hello";
}
/**
* 转发jsp页面,方法二:使用返回值为ModelAndValue
* 创建模型视图对象modelAndView,将转发的jsp路径和转发参数放入modelAndView,返回modelAndView对象
*
*/
@RequestMapping("sayHelloJsp1_4")
public ModelAndView sayHelloJsp1_4(HttpServletRequest request,HttpServletResponse response){
ModelAndView mav = new ModelAndView();
// 放入转发参数
mav.addObject("hello", "sayHelloJsp1_4");
mav.addObject("kitty", "kitty is a cat4.");
// 放入jsp路径
mav.setViewName("say_hello");
return mav;
}
/**
* 重定向:方法0。使用原始的response进行转发
*/
@RequestMapping("sayHelloJsp2_0")
public void sayHelloJsp2_0(HttpServletRequest request,HttpServletResponse response){
String url = "http://www.baidu.com";
try {
response.sendRedirect(url);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 重定向:方法1。使用返回String,使用关键字redirect:
*/
@RequestMapping("sayHelloJsp2_1")
public String sayHelloJsp2_1(HttpServletRequest request,HttpServletResponse response){
String url = "http://www.baidu.com";
return "redirect:"+url;
}
/**
* 转发到表单页面
*/
@RequestMapping("toHelloForm")
public String toHelloForm(){
return "hello_form";
}
/**
* GET请求示例
*
*/
@RequestMapping(value="toHelloWorldJsp", method=RequestMethod.GET)
public String toHelloWorldJspGet(Model model,String msg){
model.addAttribute("hello", msg);
return "say_hello";
}
/**
* post请求
*
*/
@RequestMapping(value="toHelloWorldJsp", method=RequestMethod.POST)
public String toHelloWorldJspPost(Model model,String msg){
model.addAttribute("hello", msg);
return "say_hello";
}
/**
* 返回json格式的数据:通过输出流的方式
*/
@RequestMapping(value="getHelloJson0", method=RequestMethod.GET)
public void getHelloJsp0(HttpServletResponse response,String msg){
// 创建对象
Kitty kitty = new Kitty();
kitty.setName("kitty");
kitty.setSex(1);
kitty.setMsg(msg);
// 将对象转换成json字符串
String jsonString = JSONObject.toJSONString(kitty);
// 获取输出流,并输出
try {
PrintWriter out = response.getWriter();
out.write(jsonString);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 返回json格式的数据:通过输出流的方式
*/
@RequestMapping(value="getHelloString", method=RequestMethod.GET)
@ResponseBody
public String getHelloString(String msg){
return msg;
}
/**
* 返回json格式的数据:通过@ResponseBoy方式,示例一
*/
@RequestMapping(value="getHelloJson", method=RequestMethod.GET)
@ResponseBody
public Map<String,String> getHelloJsp(String msg){
Map<String,String> result = new HashMap<>();
result.put("msg", msg);
return result;
}
/**
* 返回json格式的数据:通过@ResponseBoy方式,示例二
*/
@RequestMapping(value="getHelloJson2", method=RequestMethod.GET)
@ResponseBody
public Kitty getHelloJsp2(String msg){
Kitty kitty = new Kitty();
kitty.setName("kitty");
kitty.setSex(1);
kitty.setMsg(msg);
return kitty;
}
}
- 1
- 2
前往页