web.xml
扫描器
<context:component-scan base-package="com.yzk" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
第一种
通过对象获取
@Controller
@RequestMapping("/student")
public class StudentController {
ArrayList<Student> arrayList = new ArrayList<Student>();
@RequestMapping("/addstudent")
public String addStudent(Student student) {
System.out.println(student);
arrayList.add(student);
return "redirect:/student/showstudent.do";
}
jso页面 name 和 对象 属性相对应
第二种
Url带参
访问方式 :/student/selectstudent.do?number=1001
@RequestMapping(value = "/selectstudent", method = RequestMethod.GET)
public String selectStudentByNumber(@RequestParam("number") Integer number, Map<String, Object> map) {
System.out.println(number);
Student s = new Student(1, "yzk", number);
arrayList.add(s);
map.put("studentlist", arrayList);
return "list_student";
}
第三种
@RequestParam("number") Integer number,(int 型需要写成Interage) 获取表单 name=number的值
@RequestMapping(value = "/selectstudentbyname/{name}", method = RequestMethod.GET)
public String selectStudentByName(@RequestParam("name") String name, Map<String, Object> map) {
System.out.println(name);
Student s = new Student(1, name, 1);
arrayList.add(s);
map.put("studentlist", arrayList);
return "list_student";
}
第四种 Request
@RequestMapping(value = "/selectstudentbyname/{name}", method = RequestMethod.POST)
public String selectStudent(HttpServletRequest request, Map<String, Object> map) {
Student s = new Student(1, request.getParameter("name"), 1);
arrayList.add(s);
map.put("studentlist", arrayList);
return "list_student";
}
返回参数到jsp页面
@RequestMapping("showstudent")
public String showStudent(Map<String, Object> map) {
Student s1 = new Student(1, "小明", 1001);
Student s2 = new Student(2, "小红", 1002);
arrayList.add(s1);
arrayList.add(s2);
map.put("studentlist", arrayList);
return "list_student";
}
Mvc重定向
关键字:redirect:
例子 返回到student/showstudent.do
Spring Mvc 注解
@Controller控制器(类)
@RequestMapping("/student")
方法
@RequestMapping("/addstudent")
访问addstudent方法/student/addstudent.do