0
방금 봄에 MVC를 시작 했으므로 신참 실수 일 수 있습니다.Spring 3에서 ModelAttribute를 어떻게 재설정합니까?
ModelAttribute는 모든 요청마다 재사용됩니다. 모든 POST가 깨끗한 객체로 시작하는지 어떻게 확인할 수 있습니까?
내 컨트롤러 (MyController.java)
@Controller
public class MyController {
@RequestMapping(method = RequestMethod.POST)
public String processChoice(@ModelAttribute("myData") MyData myData, BindingResult bindingResult) {
System.out.println("POST: myData = " + myData);
return "redirect:/myview?choice=" + myData.getChoice();
}
@RequestMapping(method = RequestMethod.GET)
public String displayChoice(@RequestParam(required = false) String choice, Model model) {
System.out.println("GET: Choice = " + choice);
model.addAttribute("myData", new MyData(choice));
return "myview";
}
}
내보기 (myview.jsp)은 "선택"에
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring Tests</title>
</head>
<body>
<form:form modelAttribute="myData" method="POST">
<form:select path="choice">
<option></option>
<option value="one">One</option>
<option value="two">Two</option>
</form:select>
<input type="submit" value="Choose"/>
</form:form>
<c:if test="${not empty myData.choice}">Choice = ${myData.choice}</c:if>
<c:if test="${empty myData.choice}">No choice</c:if>
</body>
</html>
연속 클릭 버튼이 선택된 값을 추가 현재 게시물을 POST하는 대신 다음을 입력하십시오 :
GET: Choice = null
POST: myData = MyData [choice=two]
GET: Choice = two
POST: myData = MyData [choice=two,one]
GET: Choice = two,one
POST: myData = MyData [choice=two,one,]
GET: Choice = two,one,
POST: myData = MyData [choice=two,one,,one]
GET: Choice = two,one,,one
작동하지 않습니다. 이 메서드는 각 GET 및 POST 요청 전에 호출되지만 실제로 개체를 다시 설정하지는 않습니다. – Koohoolinn