防止Spring出现在预填充字段中

时间:2014-04-15 23:02:00

标签: java spring spring-mvc

我无法阻止spring填充带有值的字段。

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字段中已经有一个值。我怎样才能防止这种情况发生?

1 个答案:

答案 0 :(得分:1)

您需要重置模型以清除组ID。

尝试这样的事情。

 return new ModelAndView("newView", model);

它会创建一个新模型,以便在表单重新加载时清除该值。