2013-12-19 40 views
0

我有一個模型對象人彈簧3 MVC,不能夠訪問在JSP(封閉)

public class Person { 

     public String firstName; 
     public String lastName; 

     public String country; 
     public String sex; 
     private Integer age; 


     public String getSex() { 
      return sex; 
     } 

     public void setSex(String sex) { 
      this.sex = sex; 
     } 

     public String getCountry() { 
      return country; 
     } 

     public void setCountry(String country) { 
      this.country = country; 
     } 

     public String getLastName() { 
      return lastName; 
     } 

     public void setLastName(String lastName) { 
      this.lastName = lastName; 
     } 



     public String getFirstName() { 
      return firstName; 
     } 

     public void setFirstName(String firstName) { 
      this.firstName = firstName; 
     } 

     public Integer getAge() { 
      return age; 
     } 

     public void setAge(Integer age) { 
      this.age = age; 
     } 

     public String toString(){ 
      return firstName + " " + sex; 
     } 

} 

和在控制器I填充基準數據countryList在下文方法的模型對象

@ModelAttribute 
    public void populateCountryList(Model model){ 
     System.out.println("inside populateCountryList"); 
     Map<String,String> country = new LinkedHashMap<String,String>(); 
     country.put("Select", "-----Select------"); 
     country.put("US", "United Stated"); 
     country.put("CHINA", "China"); 
     country.put("SG", "Singapore"); 
     country.put("MY", "Malaysia"); 
     country.put("MY1", "India"); 
     country.put("MY2", "UK"); 
     country.put("MY3", "SA"); 
     country.put("MY4", "Newzeland"); 

     model.addAttribute("countryList", country);  

    } 

也填充了在另一種方法中的對象

@ModelAttribute 
    public Person populateModel(){ 
     System.out.println("inside populateCountry"); 
     Person person = new Person(); 
     person.setCountry("India"); 
     person.setSex("M"); 
     return person; 
    } 

現在在jsp中,我的組件是名字,年齡,國家下拉菜單和性別單選按鈕的文本框。我想單選按鈕性別(M)並在下拉列表中選擇「印度」默認爲選中狀態。我的jsp如下。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
... 
<h1>Person page</h1> 
<p>This is Person page</p> 
<form:form method="POST" commandName="person" action="process-person.html"> 
<table> 
    <tbody><tr> 
     <td><form:label path="firstName">Name:</form:label></td> 
     <td><form:input path="firstName"></form:input></td> 
    </tr> 


    <tr> 
     <td><form:label path="age">Age:</form:label></td> 
     <td><form:input path="age"></form:input></td> 
    </tr> 

    <tr> 
    <td><form:label path="country">Country:</form:label></td> 
    <td> 
     <form:select path="country">     
         <form:options items="${countryList}" /> 
     </form:select>  
     </td> 
    </tr> 

    <tr> 
     <td>Sex :</td> 
     <td><form:radiobutton path="sex" value="M" />Male 
      <form:radiobutton path="sex" value="F" />Female 
     </td> 
    </tr> 

    <tr> 
     <td colspan="2"> 
      <input type="submit" value="Submit"> 
     </td> 
     <td></td> 
     <td></td> 
    </tr> 
</tbody></table>  
</form:form> 

當我運行的應用程序的默認值沒有被選中。我也嘗試在jsp中打印Person對象,它正在打印它的屬性的Person對象值變爲null。

請告訴我這個實現有什麼問題。

解決方案 在處理程序方法我寫了下面的代碼。

**@RequestMapping(value="/person-form") 
public ModelAndView personPage() { 
    return new ModelAndView("person-page", "person", new Person()); 
}** 

所以我已經改變了代碼

**@RequestMapping(value="/person-form") 
public ModelAndView personPage() { 
    return new ModelAndView("person-page"); 
}** 
+1

是填充被調用的方法? –

+0

是的兩個填充方法都被調用。 – sumit

+0

我們可以看到你的處理程序方法嗎? –

回答

0

這是因爲你需要設置鍵,而不是價值的人對象:

person.setCountry("MY1");

+0

嗨Ralph,我在設置KEY後試過,但仍然沒有選擇默認值。 – sumit