2017-08-08 4 views
2

私はThymeleafを初めて使用していますが、最初のレコードを削除しようとしていますが、http://localhost:8080/findall?_method=deleteにリダイレクトされています。私はすべてのレコードを削除することができますが、彼らは適切なURL http://localhost:8080/delete/{id}にリダイレクトしています(拳1を除く)。HTMLの最初の送信ボタンが別のURLにリダイレクトしています

郵便配達員から「http://localhost:8080/delete/ {id}」を押してレコードを削除しようとすると、そのレコードを削除することができます。したがって、これは.htmlファイルで何かする必要があります。ここで

はdisplay.jspファイルのコードです:私は、削除ボタンをクリックすると

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head>Registration Table 
</head> 
     <body>` 
     `<form> 
     <table border="1"> 

     <tr> 
      <th>Id</th> 
      <th>Name</th> 
      <th>Password</th> 
      <th>Delete</th> 
     </tr> 
     <tr th:each="list : ${list}"> 
      <td th:text="${list.id}">ID</td> 
      <td th:text="${list.userName}">Name</td> 
      <td th:text="${list.userPassword}">Password</td> 
      <!-- <td><a th:href="@{/delete/{id}}">Edit</a></td> --> 

      <td> 
       <form th:action="@{'/delete/{id}'(id=${list.id})}" 
        th:method="delete"> 
        <input type="submit" value="Delete" /> 
       </form> 
      </td> 
     </tr> 
    </table> 
</form> 

、ここでは

package com.spring.controller; 

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; 

import java.util.List; 

import javax.servlet.http.HttpServlet; 
import javax.xml.ws.Response; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.view.RedirectView; 

import com.couchbase.client.deps.io.netty.handler.codec.http.HttpResponse; 
import com.spring.model.Customer; 
import com.spring.repository.CustomerRepo; 

@RestController 
public class Controller extends HttpServlet { 

    @Autowired 
    CustomerRepo rp; 

    @RequestMapping(method = RequestMethod.GET, value = "/findall") 
@ResponseBody 
public ModelAndView findall() { 
    List<Customer> list = (List<Customer>) rp.findAll(); 
    ModelAndView mv = new ModelAndView(); 
    mv.setViewName("display"); 
    mv.addObject("list", list); 
    for (Customer element : list) { 
     System.out.println("Element usernane: " + element.getUserName()); 
     System.out.println("Element password: " + element.getUserPassword()); 
    } 
    mv.addObject("list", list); 

    return mv; 
} 


    @RequestMapping(method = RequestMethod.GET, value = "/display") 
    @ResponseBody 
    public List<Customer> display() { 
     return (List<Customer>) rp.findAll(); 
    } 

    @RequestMapping("/") 
    public ModelAndView home() { 
     System.out.println("display home"); 
     ModelAndView mv = new ModelAndView("home"); 
     return mv; 
    } 


    @RequestMapping(method = RequestMethod.DELETE, value = "/delete/{id}") 
    public ModelAndView deleteCourse(@PathVariable Long id) { 
     System.out.println("deleting" + id); 
     rp.delete(id); 
     ModelAndView mv = new ModelAndView("redirect:home"); 
     return new ModelAndView("redirect:home"); 
    } 

} 

enter image description here

controller.javaです最初のレコード "Ravi"を削除するにはm eをURL「http://localhost:8080/findall?_method=delete」に変更し、レコードを削除しないでください(私はURL「http://localhost:8080/delete/1」に連れて行きます)。しかし、他のボタンをクリックしてもうまくいき、レコードを削除しています。

ブラウザのHTMLコードも確認しました。各レコードに同じHTMLコードが生成されるわけではありません。 これは非常に簡単な修正でなければなりませんが、私は、stackoverflowのThymeleafで多くのコンテンツを見つけられませんでした。

+0

htmlファイルが疑わしい場合は、ブラウザでそのファイルを見てください - 生成されたhtmlファイルと実際に生成された*実際のリンクを確認してください。 (あなたのブラウザでは、 "ソースを見る"になっています - Thymeleafによって生成された実際のHTMLファイルが表示されます) –

答えて

2

method属性は、値としてGET or POSTのみを受け入れます。

あなたは「ポスト」にth:method値を変更し、コントローラであなたのマッピングを更新することができます。

@RequestMapping(method = RequestMethod.POST, value = "/delete/{id}") 

更新 はまた、あなたのテーブルを囲む<form>を削除してください。

+0

それを試しましたが、問題を再現しませんでした。 –

+0

私には疑わしいものが2つありますが、あなたのページに影響を与えてそのような問題を引き起こすかどうかはわかりません。最初のものはテーブル全体を囲む '

'です。これは何のため?フォームはネストすることはできません。二つ目は 'list'モデル属性変数(' th:each 'の中)と同じイテレータですが、これが重要かどうかを知るための文書はありませんでした。 –

+0

ありがとうございました!解決されたタグを削除してください。 –

0

私はHTMLで2つのフォームを使用していました。 HTMLコードから外部の<form>タグを削除すると、問題が解決しました。