天天看點

spring MVC要注意的地方 2

大家可以先看我之前的一篇部落格:http://hw1287789687.iteye.com/blog/1947150

(1)送出表單時報錯:

spring MVC要注意的地方 2

 原因:

送出表單時,有個radio:

spring MVC要注意的地方 2

<label>展示狀态:</label>  

                      <input type="radio" value="on" name="status"/><span>開啟</span>  

                      <input type="radio" value="off" name="status"/><span>關閉</span>  

 對應的實體類中news 中的status.實體類代碼(省略getter,setter方法):

spring MVC要注意的地方 2

package com.ct.entity;  

import java.io.serializable;  

import java.sql.clob;  

import java.sql.timestamp;  

import javax.persistence.entity;  

import javax.persistence.generatedvalue;  

import javax.persistence.id;  

import javax.persistence.table;  

/*** 

 *  

 * @author huangwei 

 * @since 2014年9月4日 

 */  

@entity  

@table(name = "t_news")  

public class news implements cloneable,serializable{  

    private int id;  

    /*** 

     * 新聞标題 

     */  

    private string title;  

     * 開始時間 

    private string starttime;  

     * 結束時間 

    private string endtime;  

     * 新聞詳情 

    private string content;  

     * 釋出時間 

    private timestamp releasetime;  

     * 開啟或者關閉 

    private int status;  

     * 是否置頂 

    private int sticktop;  

public news clone()throws clonenotsupportedexception{  

        return (news)super.clone();  

    }  

@override  

    public string tostring() {  

        return "news [title=" + title + ", starttime=" + starttime  

                + ", endtime=" + endtime + ", content=" + content + ", status="  

                + status + "]";  

}  

 控制器中接收參數的方法:

spring MVC要注意的地方 2

@requestmapping(value = "/save")  

    public string addsaveostype(news news, model model) {  

        this.newsdao.add(news);  

        return redirectviewall;  

 news的status的類型是int,但是送出的是"off"或"on",是字元串,是以報錯.

即根本原因:送出的類型與實體類的類型不一緻,一個是string,一個是int

之前的一篇部落格:http://hw1287789687.iteye.com/blog/1947150

(2)