java - 防止 Spring 预填充字段

标签 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}";
    }

编辑:在我的返回语句中添加,因为我在 Controller 中保留了该语句和更多方法

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 字段中已经有一个值。如何防止这种情况发生?

最佳答案

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

尝试这样的事情。

 return new ModelAndView("newView", model);

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

关于java - 防止 Spring 预填充字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23096078/

相关文章:

java - 这会被认为是糟糕的 RESTFul 设计吗?

java - Spring mvc分页如何获取结果

java空值检查语法差异

java - 类构造函数在类加​​载时无法抛出异常

java - 无法解析配置类,Spring

java - HttpExchange 发送文件

java - 没有 JDBC 类型 : 1111 的方言映射

java - Spring 入站 channel 适配器 - 如何自动删除超过 10 天的文件夹和文件

java - JSESSIONID 的值在 session 失效时不会更改

java - jdbctemplate,jpatemplate