2014-04-15 90 views
0

我無法阻止彈出字段填充值。防止預填充字段彈出

Controller.java

@RequestMapping(value="helpgroups/helpsections/{id}" , method = RequestMethod.GET) 
public String listHgHelpSections(@ModelAttribute("helpSection") HelpSection helpSection, HelpGroup helpGroup,@PathVariable("id") Long id, BindingResult bindingResult) 
{ 
    helpGroup.setId(id); 
    //info needed to connect to sql server 
    final String dbUserName = "AAISdirectHelpUser"; 
    final String dbPassword = "Wr6Vaswa"; 
    final String dbURL = "jdbc:sqlserver://127.0.0.1;database=AAISdirectHelp"; 

    // sql server connection 
    try 
    { 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
     Connection conn = DriverManager.getConnection(dbURL, dbUserName, dbPassword); 
     String sql ="SELECT * FROM config.HelpSection WHERE HelpGroupID = ? "; 
     PreparedStatement ps= conn.prepareStatement(sql); 
     ps.setLong(1, helpGroup.getId()); 
     ResultSet results = ps.executeQuery(); 
     while(results.next()) 
     { 
      helpSection = new HelpSection(results.getString("Name"), results.getLong("HelpSectionID"), results.getString("Description"), results.getLong("HelpGroupID")); 
      helpGroup.getHelpSections().add(helpSection); 
     } 
catch(SQLException e1) 
     { 
      e1.printStackTrace(); 
     } 
     catch(ClassNotFoundException e2) 
     { 
      e2.printStackTrace(); 
     } 
     catch(Exception e3) 
     { 
      e3.printStackTrace(); 
     } 


     return "redirect:/helpgroups/helpsections/{id}"; 

    } 

//mapping to insert a new help section record 
    @RequestMapping("/helpgroups/helpsections/{helpgroupid}/inserthelpsection") 
    public String insertHelpSection(@ModelAttribute("helpSection") HelpSection helpSection,@PathVariable("helpgroupid") long helpGroupID, BindingResult bindingResult) 
    { 

     final String dbUserName = "AAISdirectHelpUser"; 
     final String dbPassword = "Wr6Vaswa"; 
     final String dbURL = "jdbc:sqlserver://127.0.0.1;database=AAISdirectHelp"; 
     Connection conn = null; 

     try 
     { 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      conn = DriverManager.getConnection(dbURL, dbUserName, dbPassword); 
      String insertSQL ="INSERT INTO config.HelpSection (Name, Description, HelpGroupID,Sequence) VALUES (?,?,?,?)"; 
      PreparedStatement ps = conn.prepareStatement(insertSQL); 
      ps.setString(1, helpSection.getName()); 
      ps.setString(2, helpSection.getDescription()); 
      ps.setLong(3, helpGroupID); 
      ps.setLong(4, 0); 
      ps.executeUpdate(); 

     } 

     catch(SQLException e1) 
     { 
      e1.printStackTrace(); 
     } 
     catch(ClassNotFoundException e2) 
     { 
      e2.printStackTrace(); 
     } 
     catch(Exception e3) 
     { 
      e3.printStackTrace(); 
     } 

     return "redirect:/helpgroups/helpsections/{id}"; 
    } 

編輯:在我return語句添加爲我已經離開其關閉,並在我的控制器的更多方法

package com.aais.helpguides; 


public class HelpSection 
{ 
    private long id; 
    private long helpGroupID; 
    private int sequence; 
    private String name; 
    private String description; 



    //default constructor 
    public HelpSection() 
    { 
    } 

    //constructor with specific arguments 
    public HelpSection(String name, long id, String description, long helpGroupID) 
    { 
     this.name = name; 
     this.id = id; 
     this.description = description; 
     this.helpGroupID = helpGroupID; 
    } 
    public long getHelpGroupID() 
    { 
     return helpGroupID; 
    } 
    public void setHelpGroupID(long helpGroupID) 
    { 
     this.helpGroupID = helpGroupID; 
    } 
    public long getId() 
    { 
     return id; 
    } 
    public void setId(long id) 
    { 
     this.id = id; 
    } 
    public int getSequence() 
    { 
     return sequence; 
    } 
    public void setSequence(int sequence) 
    { 
     this.sequence = sequence; 
    } 
    public String getName() 
    { 
     return name; 
    } 
    public void setName(String name) 
    { 
     this.name = name; 
    } 
    public String getDescription() 
    { 
     return description; 
    } 
    public void setDescription(String description) 
    { 
     this.description = description; 
    } 



} 

編輯:在我的HelpSection.java類文件添加

我的JSP文件看起來像這樣:

<!-- iterate over the arraylist --> 
     <c:forEach var="section" items="${helpGroup.helpSections}"> 
      <tr> 
       <td>${section.id}</td> 
       <td><a href="helpsections/helpinfo/${section.id}">${section.name}</a></td> 
       <td>${section.description}<td> 
      </tr> 
     </c:forEach>  
    </table> 

    <hr> 
     <h2>Insert Help Section</h2> 
      <form:form method="post" action="${id}/inserthelpsection" modelAttribute="helpSection"> 
       <table style="width:750px"> 
        <tr> 
         <td>Help Section Name:</td> 
         <td><form:input type="text" path="name"/></td> 
         <td>Description:</td> 
         <td><form:input type="text" path="description" /></td> 
         <%-- <td>Sequence:</td> 
         <td><form:input type="text" path="sequence"/> --%> 
         <td><input type="submit" value="Insert"/></td> 
        </tr> 
       </table> 
      </form:form> 

所以當窗體出現時,幫助組標識字段已經有一個值。我怎樣才能防止這種情況發生?

+0

擺脫查詢的代碼 – fmodos

+0

你如何指責Spring?你的控制器還有其他什麼方法? –

+0

請注意,'BindingResult'應該在參數列表中的相關參數之後。 –

回答

1

您需要重置清除組ID的模型。

嘗試類似這樣的東西。

return new ModelAndView("newView", model); 

它創建一個新的模型,這樣值將在表單重新加載時清除。

+0

我試過像你說的那樣返回一個新的modelandview,但它只是在我的表單中填充了更多的字段。我可能誤解了你的意思。我添加了以下內容:返回新的ModelAndView(「helpguides/showHelpSections」,「helpSection」,helpSection)。你能否澄清一下你想重置模型的意思? – atsprink