@ModelAttribute在发布

时间:2015-10-30 05:43:22

标签: spring hibernate spring-mvc

您好我正在开发我的第一个春季mvc应用程序。我正在尝试向db添加表单。为此我使用@ModelAttribute并且它无法正常工作。

我的控制器:

@Controller
public class AddKPI {

    @RequestMapping("/getAddKPIForm")
    public ModelAndView addKPI(){
        ModelAndView modelAndView =  new ModelAndView("getAddKPIForm","kpi", new KPI());
        String auditInfo = "User : "+getCurrentDateInStringFormat_YYYYMMDD_HHSSMM();
        modelAndView.addObject("auditInfo",auditInfo);

        return modelAndView;
    }

    @RequestMapping(value="/addKPITODB")
    public ModelAndView add2DB(@ModelAttribute("kpi") KPI kpi){
        System.out.println("In controller Method");
        System.out.println("kpi.getKpi_name() = "+kpi.getKpi_name());
        return new ModelAndView("adding_Success");
    }
    public static String getCurrentDateInStringFormat_YYYYMMDD_HHSSMM() {
        String date_Str = null;
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            date_Str = sdf.format(date);
        } catch (Exception pe) {
            // TODO: Add catch code
            pe.printStackTrace();
        }
        return date_Str;
    }
}

我的表格代码:

<div align="center">
    <div style="width: 650px;">
        <form:form class="form-horizontal" method="POST" modelAttribute="kpi" commandName="kpi" action="addKPITODB" >
            <form:input type="hidden" name="id" value="122" path="id"></form:input>
            <div class="form-group form-group-sm">

                <label for="kpi_name" class="col-sm-2 control-label">KPI
                    Name</label>
                <div class="col-sm-5">
                    <form:input type="text" class="form-control" id="kpi_name"
                        placeholder="KPI Name" name="kpi_name" required="required"
                        autofocus="autofocus" path="kpi_name"/>
                </div>

            </div>
            <div class="form-group  form-group-sm">
                <label for="kpi_sql" class="col-sm-2 control-label">KPI SQL</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" placeholder="K sql"
                        name="kpi_sql" id="kpi_sql" required="required">
                </div>
            </div>
            <div class="form-group  form-group-sm">
                <label for="threshold" class="col-sm-2 control-label">Threshold</label>
                <div class="col-sm-2">
                    <input type="text" class="form-control" placeholder="t"
                        name="threshold" id="threshold" required="required">
                </div>

                <label for="Operator" class="col-sm-3 control-label">Threshold
                    Operator</label>
                <div class="col-xs-3">
                    <select class="form-control input-small" id="Operator"
                        name="Operator">
                        <option>G Than</option>
                        <option>L Than</option>
                    </select>
                </div>
            </div>
            <div class="form-group form-group-sm">
                <label for="category" class="col-sm-2 control-label">Category</label>
                <div class="col-xs-3">
                    <select class="form-control input-small" id="category"
                        name="category">
                        <option value="Business Health">Business Health</option>
                        <option value="Operations Health">Operations Health</option>
                    </select>
                </div>
            </div>
            <div class="form-group form-group-sm">
                <label for="module" class="col-sm-2 control-label"> Module</label>
                <div class="col-xs-3">
                    <select class="form-control input-small" id="module">
                        <option value="Finance">Finance</option>
                        <option value="Supply Chain">Supply Chain</option>
                    </select>
                </div>
            </div>
            <div class="form-group form-group-sm">
                <label for="submodule" class="col-sm-2 control-label">Sub
                    Module</label>
                <div class="col-xs-3">
                    <select class="form-control input-small" id="submodule"
                        name="submodule">
                        <option value="A/P (Accounts Payable)">A/P (Accounts
                            Payable)</option>
                        <option value="A/R (Accounts Receivable)">A/R (Accounts
                            Receivable)</option>
                        <option value="Inventory">Inventory</option>
                        <option value="Purchase">Purchase</option>
                        <option value="Sales">Sales</option>
                        <option value="Manufacturing">Manufacturing</option>
                    </select>
                </div>
            </div>
            <div class="form-group  form-group-sm">
                <label for="helptext" class="col-sm-2 control-label">Help
                    Text</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" placeholder="helptext"
                        name="helptext" id="helptext" required="required">
                </div>
            </div>
            <div class="form-group  form-group-sm">
                <label for="perf_reason" class="col-sm-2 control-label">Reason</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" placeholder="Reason"
                        name="perf_reason" id="perf_reason" required="required">
                </div>
            </div>
            <!-- category , module, submodule -->

            <!-- Operator , AutoMoniter_Enabled, submodule -->

            <div class="form-group  form-group-sm">
                <label for="Xaxis_Description" class="col-sm-2 control-label">Xaxis_Description</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control"
                        placeholder="Xaxis_Description" name="Xaxis_Description"
                        id="Xaxis_Description" required="required">
                </div>
            </div>
            <div class="form-group  form-group-sm">
                <label for="Yaxis_Desciption" class="col-sm-2 control-label">Yaxis_Desciption</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control"
                        placeholder="Yaxis_Desciption" name="Yaxis_Desciption"
                        id="Yaxis_Desciption" required="required">
                </div>
            </div>
            <div class="form-group  form-group-sm">
                <label for="toEmail" class="col-sm-2 control-label">To
                    Email</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" placeholder="toEmail"
                        name="toEmail" id="toEmail" required="required">
                </div>

                <label for="cc_email" class="col-sm-2 control-label">CC
                    Email</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" placeholder="cc_email"
                        name="cc_email" id="cc_email" required="required">
                </div>
            </div>
            <div class="form-group  form-group-sm">
                <label for="DSchemaName" class="col-sm-2 control-label">Detailed
                    View</label>
                <div class="col-sm-3">
                    <input type="text" class="form-control" placeholder="DSchemaName"
                        name="DSchemaName" id="DSchemaName">
                </div>

                <div class="col-sm-3">
                    <input type="text" class="form-control" placeholder="DTableName"
                        name="DTableName" id="DTableName">
                </div>

                <div class="col-sm-4">
                    <input type="text" class="form-control" placeholder="DFieldName"
                        name="DFieldName" id="DFieldName">
                </div>
            </div>
            <div class="form-group form-group-sm">
                <label for="ame" class="col-sm-2 control-label">Auto
                    Mointer Enabled</label>
                <div class="col-xs-3">
                    <select class="form-control input-small" id="ame"
                        name="AutoMoniter_Enabled">
                        <option value="Yes">Yes</option>
                        <option value="No">No</option>
                    </select>
                </div>
            </div>
            <div class="form-group  form-group-sm">
                <label for="Audit Info" class="col-sm-2 control-label">Audit
                    Info</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" placeholder="Audit Info"
                        name="audit_info" id="Audit Info" readonly="readonly"
                        value="${auditInfo }">
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <div align="left">
                        <button type="submit" class="btn btn-primary btn-xs">Add
                            KPI</button>
                    </div>
                </div>
            </div>
        </form:form>
    </div>
</div>

MY KPI课程:

@Entity
@Table(name="KPI")
public class KPI {
    @Id
    private int id;
    public KPI(int id) {
        super();
        this.id = id;
    }
    public KPI(){

    }
    private String kpi_name;
    private String kpi_sql;
    private int threshold;
    private String helptext;
    private String perf_reason;
    private String category;
    private String module;
    private String submodule;
    private String status;
    private String toEmail;
    private String Operator;
    private String AutoMoniter_Enabled;
    private String cc_email;
    private String audit_info;
    private String Xaxis_Description;
    private String Yaxis_Desciption;
    private String DSchemaName;
    private String DTableName;
    private String DFieldName;

    public String getXaxis_Description() {
        return Xaxis_Description;
    }
    public void setXaxis_Description(String xaxis_Description) {
        Xaxis_Description = xaxis_Description;
    }
    public String getYaxis_Desciption() {
        return Yaxis_Desciption;
    }
    public void setYaxis_Desciption(String yaxis_Desciption) {
        Yaxis_Desciption = yaxis_Desciption;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getKpi_name() {
        return kpi_name;
    }
    public void setKpi_name(String kpi_name) {
        this.kpi_name = kpi_name;
    }
    public String getKpi_sql() {
        return kpi_sql;
    }
    public void setKpi_sql(String kpi_sql) {
        this.kpi_sql = kpi_sql;
    }


    public int getThreshold() {
        return threshold;
    }
    public void setThreshold(int threshold) {
        this.threshold = threshold;
    }
    public String getHelptext() {
        return helptext;
    }
    public void setHelptext(String helptext) {
        this.helptext = helptext;
    }
    public String getPerf_reason() {
        return perf_reason;
    }
    public void setPerf_reason(String perf_reason) {
        this.perf_reason = perf_reason;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public String getModule() {
        return module;
    }
    public void setModule(String module) {
        this.module = module;
    }
    public String getSubmodule() {
        return submodule;
    }
    public void setSubmodule(String submodule) {
        this.submodule = submodule;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getToEmail() {
        return toEmail;
    }
    public void setToEmail(String toEmail) {
        this.toEmail = toEmail;
    }
    public String getOperator() {
        return Operator;
    }
    public void setOperator(String operator) {
        Operator = operator;
    }
    public String getAutoMoniter_Enabled() {
        return AutoMoniter_Enabled;
    }
    public void setAutoMoniter_Enabled(String autoMoniter_Enabled) {
        AutoMoniter_Enabled = autoMoniter_Enabled;
    }
    public String getCc_email() {
        return cc_email;
    }
    public void setCc_email(String cc_email) {
        this.cc_email = cc_email;
    }
    public String getAudit_info() {
        return audit_info;
    }
    public void setAudit_info(String audit_info) {
        this.audit_info = audit_info;
    }
    public String getDSchemaName() {
        return DSchemaName;
    }
    public void setDSchemaName(String dSchemaName) {
        DSchemaName = dSchemaName;
    }
    public String getDTableName() {
        return DTableName;
    }
    public void setDTableName(String dTableName) {
        DTableName = dTableName;
    }
    public String getDFieldName() {
        return DFieldName;
    }
    public void setDFieldName(String dFieldName) {
        DFieldName = dFieldName;
    }

}

当我点击提交按钮时,它不会转到控制器方法&#34; / addKPITODB&#34;我收到404错误(网址为http://localhost:6006/EasyRUN/addKPITODB)。如果我删除@ModelAttribute,它将转到控制器方法。我在做什么错。请纠正我。

非常感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

我使用您的代码创建了一个示例项目,该工作正常:

  1. git clone https://github.com/denis-zhdanov/spring-playground.git
  2. cd spring-playground /
  3. git checkout so-33429407
  4. export GRADLE_OPTS =“ - Xdebug -Xrunjdwp:transport = dt_socket,address = 9999,server = y,suspend = n”
  5. ./ gradlew jettyRun
  6. 等到打印以下行 - ':jettyRun&gt;在http://localhost:8080/spring-playground'
  7. 上运行
  8. 通过远程调试器连接到localhost:9999
  9. 在org.denis.test.spring.mvc.MyController.add2DB()
  10. 设置断点
  11. 在浏览器中打开“http://localhost:8080/spring-playground/form.html
  12. 在那里输入任何文字,然后按
  13. 断点被击中