Java 防止Spring预填充字段

Java 防止Spring预填充字段,java,spring,spring-mvc,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

我无法阻止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}";
    }
Edit:在我的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;
    }



}
编辑:添加到my 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>

${section.id}
${section.description}

插入帮助部分 帮助部分名称: 说明:

因此,当表单显示时,帮助组id字段中已经有一个值。如何防止这种情况发生?

您需要重置模型以清除组id

试试这样的

 return new ModelAndView("newView", model);

它创建了一个新的模型,这样在重新加载表单时,值将被清除。

去掉执行查询的代码,您如何责怪Spring?控制器中还有哪些其他方法?请注意,
BindingResult
应该紧跟在参数列表中与其相关的参数之后。@fmodos为什么要删除查询代码?我需要它来从数据库中获取信息以显示我试图返回一个新的model和view,就像你说的那样,但它只是在我的表单中填充了更多的字段。我可能误解了你的意思。我添加了以下内容:returnnewmodelandview(“helpguides/showHelpSections”、“helpSection”、“helpSection”)。你能再澄清一下你想重置模型的内容吗?