2014-04-15 4 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}"; 
    } 

편집 : 내 반환 문에서 추가 나는 그것을 중단하고 한대로 내 컨트롤러에 더 많은 방법

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> 

폼이 나타나면 도움말 그룹 ID 필드에 값이 이미 있습니다. 어떻게 이런 일이 일어나지 않도록 할 수 있습니까?

+0

쿼리를 수행하는 코드를 제거하십시오. – fmodos

+0

그리고 스프링을 어떻게 비난합니까? 그리고 컨트롤러의 다른 방법은 무엇입니까? –

+0

'BindingResult'는 매개 변수 목록에 관련된 매개 변수 바로 다음에 와야합니다. –

답변

1

그룹 ID를 지우려면 모델을 재설정해야합니다.

다음과 같이 시도해보십시오.

return new ModelAndView("newView", model); 

새 모델을 작성하므로 양식을 다시로드 할 때 값이 지워집니다.

+0

나는 네가 의미하는 바를 오해했을지도 모른다. 다음을 추가했습니다 : 새로운 ModelAndView ("helpguides/showHelpSections", "helpSection", helpSection)를 반환하십시오. 모델 재설정을위한 의미를 조금 더 명확하게 설명해 주시겠습니까? – atsprink

관련 문제