제2 장 Spring MVC 추가 검사 사례
이 장 은 spring mvc 를 사용 하여 student 대상 의 첨삭 을 모 의 하 는 작은 예 입 니 다.
1.spring mvc 주석:
1.@Controller 성명 컨트롤 러
@Controller
public class StudentController {
//....
}
2,@RequestMapping 요청 맵/요청 경로
@RequestMapping("/student")
public class StudentController {
// , /student
@RequestMapping("/list")
public ModelAndView list() {
// , /student/list
}
}
3,@RequestParam 요청 매개 변수
@RequestMapping("/preSave")
public ModelAndView preSave(@RequestParam(value = "id", required = false) String id){
// id
}
@RequestMapping("/save")
public ModelAndView save(Student student){
// ,
}
4.Model AndView 는 모델 과 보 기 를 되 돌려 줍 니 다.
@RequestMapping("/delete")
public ModelAndView delete(Student student) {
studentMap.remove(student.getId());
ModelAndView mav = new ModelAndView();
mav.addObject("studentList", studentMap);
mav.setViewName("student/list");
return mav;
}
2.spring 중국어 인 코딩 필터,웹.xml 에 추가;
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
3.jsp 는 HashMap 의 대상 을 옮 겨 다 니 며 대상 의 속성 을 가 져 옵 니 다.
${student.value.id }
${student.value.name }
${student.value.age }
수정 하 다.
삭제
4.Model AndView 는 모델 과 보 기 를 되 돌려 줍 니 다.
/**
*
*
* @param id
* @return
*/
@RequestMapping("/preSave")
public ModelAndView preSave(
@RequestParam(value = "id", required = false) String id) {
ModelAndView mav = new ModelAndView();
if (id != null) {
mav.addObject("student", studentMap.get(Integer.parseInt(id)));
mav.setViewName("student/update");
} else {
mav.setViewName("student/add");
}
return mav;
}
5.SpringMVC 대상 속성 자동 봉인
/**
* , ,
*
* @param student
* @return
*/
@RequestMapping("/save")
public String save(Student student) {
if (student.getId() == null) {
// id 1
identityId += 1;
student.setId(identityId);
studentMap.put(student.getId(), student);
} else {
//
studentMap.put(student.getId(), student);
}
//
return "redirect:/student/list.do";
//
// return "redirect:/student/list.do";
}
6.내부 재 설정 과 전송:
/**
*
*
* @param student
* @return
*/
@RequestMapping("/delete")
public String delete(@RequestParam("id") Integer id) {
studentMap.remove(id);
//
return "redirect:/student/list.do";
//
// return "redirect:/student/list.do";
}
프로젝트 원본:
web.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
SpringMvc02
index.jsp
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
springmvc
*.do
spring-mvc.xml
Student.java
package com.fx.model;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
super();
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
StudentController.java
package com.fx.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.fx.model.Student;
/**
* Controller: Controller RequestMapping:
*
* @author fx
*
*/
@Controller
@RequestMapping("/student")
public class StudentController {
private Integer identityId = 3;
private static Map studentMap = new HashMap();
static {
studentMap.put(1, new Student(1, " ", 11));
studentMap.put(2, new Student(2, " ", 12));
studentMap.put(3, new Student(3, " ", 13));
}
/**
*
*
* @return
*/
@RequestMapping("/list")
public ModelAndView list() {
ModelAndView mav = new ModelAndView();
mav.addObject("studentList", studentMap);
mav.setViewName("student/list");
return mav;
}
/**
*
*
* @param id
* @return
*/
@RequestMapping("/preSave")
public ModelAndView preSave(
@RequestParam(value = "id", required = false) String id) {
ModelAndView mav = new ModelAndView();
if (id != null) {
mav.addObject("student", studentMap.get(Integer.parseInt(id)));
mav.setViewName("student/update");
} else {
mav.setViewName("student/add");
}
return mav;
}
/**
* , ,
*
* @param student
* @return
*/
@RequestMapping("/save")
public String save(Student student) {
if (student.getId() == null) {
// id 1
identityId += 1;
student.setId(identityId);
studentMap.put(student.getId(), student);
} else {
//
studentMap.put(student.getId(), student);
}
//
return "redirect:/student/list.do";
//
// return "redirect:/student/list.do";
}
/**
*
*
* @param student
* @return
*/
@RequestMapping("/delete")
public String delete(@RequestParam("id") Integer id) {
studentMap.remove(id);
//
return "redirect:/student/list.do";
//
// return "redirect:/student/list.do";
}
}
list.jsp
Insert title here
학생
번호성명.
나이.
조작 하 다.
${student.value.id }
${student.value.name }
${student.value.age }
수정 하 다.
삭제
add.jsp
Insert title here
학생성명.
나이.
update.jsp
Insert title here
학생 수정성명.
나이.
접근 경로:
http://localhost:8080/SpringMvc02/student/list.do
다운로드 횟수:0
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.