graph TD
A[DisptcherServlet] --> B[HandlerMapping]
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springmvc
/
package wz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("demo")
public String demo(){
return "main.jsp";
}
}
在web.xml中添加过滤器
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
encoding
/*
@Controller
public class DemoController {
@RequestMapping("demo")
public String demo(String name, int age) {
System.out.println(name + " " + age);
return "main.jsp";
}
@RequestMapping("demo")
public String demo(@RequestParam(value="name1") String name ,@RequestParam(value="age1") int age){
System.out.println("执行 demo"+" "+name+" "+age);
return "main.jsp";
}
@RequestMapping("page")
public String page(@RequestParam(defaultValue = "2") int pageSize, @RequestParam(defaultValue = "1") int pageNumber) {
System.out.println(pageSize + " " + pageNumber);
return "main.jsp";
}
@RequestMapping("demo2")
public String demo2(@RequestParam(required = true) String name) {
System.out.println("name是SQL的查询条件" + name);
return "main.jsp";
}
@RequestMapping("demo4")
public String demo4(People peo){
return "main.jsp";
}
@RequestMapping("demo5")
public String demo5(String name, int age, @RequestParam("hover")List hover){
System.out.println(hover);
return "main.jsp";
}
public class Demo {
private People peo;
}
@RequestMapping("demo6")
public String demo6(Demo demo){
System.out.println(demo);
return "main.jsp";
}
public class Demo {
private List peo;
}
@RequestMapping("demo6")
public String demo6(Demo demo){
System.out.println(demo);
return "main.jsp";
}
跳转
在@RequestMapping 中一定要和请求格式对应
{名称} 中名称自定义名称
@PathVariable 获取@RequestMapping 中内容,默认按照方法参数名称去寻找.
@RequestMapping("demo8/{id1}/{name}")
public String demo8(@PathVariable String name, @PathVariable("id1") int age) {
System.out.println(name + " " + age);
return "main.jsp";
}
@RequestMapping(value="demo12",produces="text/html;charset=utf-8")
@ResponseBody
public String demo12() throws IOException{
People p = new People();
p.setAge(12);
p.setName("张三");
return "中文";
}