我面臨着和春天有個問題,它會如下:Spring MVC的:SessionAttributes和列表
在SessionAttributes我有對象者與屬性的地址是一個列表。無論何時通過控制器更新人員,先前的條目仍然存在。例如,如果我有親自的地址:舊地址1,舊地址2,舊地址3和我通過表格更新人員只有一個新地址,地址列表變爲:新地址1,舊地址2,舊地址3而預期的行爲只有「新地址1」。我似乎無法找到解決這個問題的辦法。我正在使用Spring 3.0.X.
請在下面找到顯示問題的所有相關代碼。
Person.java:
package com.convert.dashboard.web.test;
import java.util.List;
public class Person {
private String name;
private Integer age;
private List<String> addresses;
public Person(List<String> addresses) {
this.addresses = addresses;
}
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;
}
public List<String> getAddresses() {
return addresses;
}
public void setAddresses(List<String> addresses) {
this.addresses = addresses;
}
}
TestController.java
package com.convert.dashboard.web.test;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/test")
@SessionAttributes("person")
public class TestController {
@RequestMapping(value = "/")
public ModelAndView xyz() {
ModelAndView mav = new ModelAndView();
List<String> abc = new ArrayList<String>();
abc.add("old address1");
abc.add("old address2");
abc.add("old address3");
Person person = new Person(abc);
mav.addObject("person", person);
mav.setViewName("cForm");
return mav;
}
@RequestMapping("/save")
public @ResponseBody
String process(@ModelAttribute("person") Person person) {
return "<body>" + " Name:" + person.getName() + " Age: " + person.getAge() + " Addresses: " + person.getAddresses();
}
}
cForm.jsp:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>populate form</title>
</head>
<body>
<form:form modelAttribute="person" action="/dashboard/test/save">
<form:hidden path="name" value="X" />
<form:hidden path="age" value="20" />
<form:hidden path="addresses[0]" value="New address" />
<input type="Submit" value="Submit" />
</form:form>
</body>
</html>
我決不是專家,但默認情況下,類的範圍是單身。也許在你的情況下@Scope(「原型」)會更合適? – Bill 2011-02-04 17:51:04
添加@Scope(「prototype」)不能解決問題。 – Eizenhooven 2011-02-04 18:01:54
你爲什麼想讓這個人留在會議中。除非幾乎必要,否則在會話中存儲某些內容並不是一個好主意。 – Javi 2011-02-04 18:08:04