2013-03-06 92 views
0

我在配置彈簧MVC以在右側控制器中處理表單POST數據時遇到問題。我有一個add行動,它將爲數據庫添加一條新記錄。彈簧進程POST數據 - 註釋正確的控制器

表單提交後,我得到404錯誤(http://localhost:8084/lyricsBase/song/submit.html),所以我想我在路由表單提交時出錯了。

這是我的控制器代碼:

public class SongController extends MultiActionController { 

    [...] 
    @RequestMapping(value = "/song/submit.html", method = RequestMethod.POST) 
    public ModelAndView submit(@RequestParam("song") Song song) throws Exception { 
     HashMap model = new HashMap(); 
     model.put("song", song); 
     // or do something better here... 
     return new ModelAndView("t.edit", model); 
    } 

,這是視圖形式標籤:

<form:form method="POST" commandName="song" action="submit.html"> 

我的應用程序的代碼可以在github。這裏是重要的文件:the form view,controller(該類是一個多控制器,因爲我不想爲每個動作創建一個單獨的文件)和servlet configuration

不知道它是否重要,但我使用瓷磚視圖層(和邏輯視圖名稱在tiles.xml中使用)。

此外,我不完全瞭解彈簧佈線如何工作。到現在爲止,我確定在servlet XML映射,但不知道這是否是一個好方法...

+0

請在您的問題中添加相關的代碼片段。 – 2013-03-06 22:51:57

+0

@KeesdeKooter添加了重要的代碼片段。 – ducin 2013-03-06 22:55:59

+0

如果使用@RequestMapping註釋映射您的請求,則不需要將它映射到您的servlet.xml中。 – 2013-03-06 23:02:36

回答

0

試試這個,改變@RequestParam("song")@RequestBody

0

如果您的應用程序的網址是:

http://localhost:8084/lyricsBase/song/submit.html 

請求映射應該是這樣(在映射首先刪除 '/'):在JSP

@RequestMapping(value = "song/submit.html", method = RequestMethod.POST) 
public ModelAndView submit(@ModelAttribute("song") Song song) throws Exception { 
} 

,並形成標籤應該是:

<form:form method="POST" commandName="song" action="song/submit.html"> 
1

什麼是歌曲的發佈價值?我不確定Spring是否將發佈的數據轉錄或反序列化爲對象/實體。 你可以嘗試改變;

@RequestMapping(value = "/song/submit.html", method = RequestMethod.POST) 
public ModelAndView submit(@RequestParam("song") Song song) throws Exception { 

@RequestMapping(value = "/song/submit.html", method = RequestMethod.POST) 
public ModelAndView submit(@RequestParam("song") String song) throws Exception { 

,看看如果多數民衆贊成回升。

另一種方式是從請求對象中讀取參數;

@RequestMapping(value = "/song/submit.html", method = RequestMethod.POST) 
public ModelAndView submit(HttpServletRequest request) throws Exception { 

Object song = request.getParameter("song"); 

GI!