04springmvc请路径参数
package com.fz.controller;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by webrx on 2017-06-26.
*/
@Controller @RequestMapping("/user")
public class User {
@RequestMapping("/t1.action") //http://localhost/user/t1.action?id=600
public ModelAndView t1(HttpServletRequest req){
String id = req.getParameter("id");
System.out.println(id);
ModelAndView mv = new ModelAndView("t1");
mv.addObject("id",id);// request.setAttribute("id",id);
return mv; ///WEB-INF/template/t1.jsp 渲染显示
}
@RequestMapping("/t2") @ResponseBody //http://localhost/user/t2.action?id=333
public void t2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset-utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>玉灵 QQ:7031633</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>id="+ req.getParameter("id") +"</h3>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
@RequestMapping("/t3")
public String t3(){
//转发 派发 不是重定向
//return "t3"; // /WEB-INF/template/t3.jsp 视图 来渲染显示数据
//转发方式2
//return "forward:t3.jsp"; //http://localhost/user/t3.jsp
//转发到 http://localhost/t3.jsp
//return "forward:/t3.jsp";
//重定向 http://localhost/t3.jsp 项目根目录
return "redirect:/t3.jsp";
//return "redirect:t3.jsp"; //http://localhost/user/t3.jsp
//重定向方式
//return "redirect:t3.action"; //重定向到 http://localhost/user/t3.action
}
@RequestMapping("/t4.action") @ResponseBody
public String t4(){
return "Hello-中国人"; //直接输出显示字符串
}
//http://localhost/user/t5.action 必须为POST
@RequestMapping(value = "t5",method = RequestMethod.POST) @ResponseBody
public void t5(){
System.out.println("hello");
}
@RequestMapping(value = "t6",method = RequestMethod.GET) @ResponseBody
public void t6(String name){
System.out.println(name);
}
//Springmvc RestFul 风格形式
http://localhost/user/news/100.html
http://localhost/user/news/102.html
http://localhost/user/news.html?id=102
// http://localhost/user/t7/100.action
// http://localhost/user/t7.action?id=100
//100 = > {id} => int id
@RequestMapping(value = "/t7/{id}",method = RequestMethod.GET) @ResponseBody
public void t7(@PathVariable int id){
System.out.println(id);
}
}
package com.fz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by webrx on 2017-06-27.
*/
@Controller
public class User {
//http://localhost/save
@RequestMapping(value = "save", method = RequestMethod.GET)
@ResponseBody
public void save() {
System.out.println("hello-save()");
}
//http://localhost/aa
//http://localhost/bb
//http://localhost/cc
@RequestMapping(value = {"aa", "bb", "cc"}, method = RequestMethod.GET)
public void show(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset-utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
out.println("<meta name=\"keywords\" content=\"关键字\">");
out.println("<meta name=\"description\" content=\"简介\">");
out.println("</head>");
out.println("<body>");
out.println("<h3>Hello World 中文效果</h3>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
//http://localhost/user
@RequestMapping("/user")
public String user(Model m) {
m.addAttribute("name", "张三丰");
m.addAttribute("age", 18);
m.addAttribute("address", new String[]{"郑州", "上海", "北京", "天津"});
return "user"; // /WEB-INF/template/user.jsp 视图渲染
}
//RESTFul 风格参数
//http://localhost/add/300/李四.action
//http://localhost/add/300/李四
@RequestMapping("/add/{id}/{name}")
@ResponseBody
public void add(HttpServletResponse resp, @PathVariable String name, @PathVariable int id) throws IOException {
//System.out.println(name);
//System.out.println(id);
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset-utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
out.println("<meta name=\"keywords\" content=\"关键字\">");
out.println("<meta name=\"description\" content=\"简介\">");
out.println("</head>");
out.println("<body>");
out.println("<h3>" + name + "</h3>");
out.println("<h3>" + id + "</h3>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
//http://localhost/insert.action?nn=jack
//http://localhost/insert
//http://localhost/insert.action?aa=38&nn=李四四
@RequestMapping(value="/insert",method = RequestMethod.GET)
public void insert(@RequestParam(value = "nn", required = false, defaultValue = "李四") String name, @RequestParam(value = "aa",required = false,defaultValue = "18") int age,HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset-utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>玉灵 QQ:7031633 Email:webrx@126.com</title>");
out.println("<meta name=\"keywords\" content=\"关键字\">");
out.println("<meta name=\"description\" content=\"简介\">");
out.println("</head>");
out.println("<body>");
out.println("<h3>"+ name +"</h3>");
out.println("<h3>"+ age +"</h3>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
}
//add.action?name=李四四&age=28
@RequestMapping("/add")
public void add(@RequestParam(value="name") String uname, @RequestParam(value="age", required=false, defaultValue="0") int age,HttpServletResponse res) throws IOException{
res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.print("年龄:"+age);
out.print("欢迎:"+uname);
out.flush();
out.close();
}
此方法如果是表单 POST方式请求传参 可以在web.xml 中配置编码过滤器 解决乱码
web.xml 配置方式
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>sm01</display-name>
<!-- web项目支持spring mvc 4.3.3 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- spring mvc 在启动时主动查找加载 src目录下也就是WEB-INF/classes 目录下mvc.xml -->
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- spring 编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>