2016-04-02 12 views
0
benim thymeleaf sayfasında şu şekilde

Thymeleaf eşdeğeri: url

<td><a class="btn btn-success" href="<c:url value="/displayWikis" />">&nbsp;&nbspShow Wiki List&nbsp;&nbsp</a></td> 

anda benim kodudur nasıl c url çalışma benzer tıklandığında ben bir mvc denetleyicisi gitmek için bir thymeleaf düğmesine almaya çalışıyorum

<td><button type="button" th:href="@{/getAll}">Get All Post </button></td> 

Bu

kontrolör kodu

thymelead sayfa İşte

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head lang="en"> 


<title>MVC Home</title> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

<link href="../static/css/bootstrap.css" th:href="@{css/bootstrap.css}" 
rel="stylesheet" media="screen" /> 

link href="../static/css/bootstrap-theme.css" 
th:href="@{css/bootstrap-theme.css}" rel="stylesheet" media="screen" /> 


<script type="text/javascript" src="../static/js/jquery-2.2.2.js" 
th:src="@{js/jquery-2.2.2.js}"></script> 


<script type="text/javascript" src="../static/js/tether.js" 
th:src="@{js/tether.js}"></script> 



<script type="text/javascript" src="../static/js/bootstrap.js" 
th:src="@{js/bootstrap.js}"></script> 


</head> 
<body> 


<div class="container"> 
    <div class="jumbotron"> 

     <h1>Form</h1> 
     <form action="#" th:action="@{/mvchome}" th:object="${post}" 
      method="post"> 

      <table> 

       <tr> 
        <td>Username:</td> 
        <td><input type="text" th:field="*{userAcctName}" /></td> 
       </tr> 
       <tr> 
        <td>City:</td> 
        <td><input type="text" th:field="*{city}" /></td> 
       </tr> 
       <tr> 
        <td>Content:</td> 
        <td><input type="text" th:field="*{content}" /></td> 
       </tr> 
       <tr> 
        <td><button type="submit" name="action" value="save">Submit post</button></td> 
        <td><button type="button" th:href="@{/getAll}">Get All Post </button></td> 

       </tr> 


      </table> 


     </form> 



    </div> 
    <div> 
    <!--  <button type="button" th:href="@{/getAll}"> Get All </button> --> 
    </div> 

    <!-- Results Block --> 

    <th:block th:each="post : ${postsList}"> 


     <div class="card "> 
      <div class="card-block"> 
       <h4 class="card-title" id="test" th:text="${post.subject}">Subject</h4> 
       <h6 class="card-subtitle text-muted" th:text="${post.created}">Date 
        Created</h6> 
      </div> 

      <div class="card-block"> 
       <p class="card-text" th:text="${post.content}">Post Data</p> 
       <a href="#" class="card-link"></a> 
       <button type="button" class="btn btn-sm btn-danger" 
        data-toggle="popover" title="Popover title" 
        data-content="th:text='${post.city}'">See Popover</button> 
      </div> 
      <div class="card-footer text-muted" th:text="${post.temperature}"></div> 
     </div> 

    </th:block> 



</div> 

<script> 
    function getAllByUser() { 

     $.ajax({ 
      type : "get", 
      url : "https://localhost:8443/api/getAllForUser", 
      cache : false, 
      data : 'user=' + $("#userAcctName").val(), 
      success : function(response) { 
       alert(response); 

       //some how pass this response data to ${postsList} 

       $('#test').html("" + response[0].subject); 

       $(".card").children().removeClass('hidden'); 
       $('[data-toggle="popover"]').popover(); 
      }, 
      error : function() { 
       alert('Error while request..'); 
      } 
     }); 
    } 

    $(document).ready(function() { 
     $('[data-toggle="popover"]').popover(); 
     //   $(".bg-success").children().addClass('hidden'); 

    }); 
</script> 

</body> 
</html> 

var olduğunu

@Controller 
public class PostController { 

private final PostService postService; 

@Inject 
public PostController(final PostService postService){ 
    this.postService=postService; 
} 



@RequestMapping(value="/", method = RequestMethod.GET) 
public String home() { 
    return "home"; 
} 


@RequestMapping(value="/mvchome", method = RequestMethod.GET) 
public String mvchome(Model model) { 
    Post post = new Post(); 

    model.addAttribute("post", post); 
    return "mvchome"; 
} 



@RequestMapping(value = "/mvchome", method=RequestMethod.POST) 
//public String create(@Valid Post post, BindingResult bindingResult, Model model) { 
public ModelAndView create(@Valid Post post) { 
//  if (bindingResult.hasErrors()) { 
//   return "mvchome"; 
//  } 
    //Post post = new Post(0, 1, 0, content,content, new Date(),userAcctName,city, null, null, null); 
    post.setParentId(0); 
    post.setDisplayOrder(1); 
    post.setIndentLevel(0); 
    post.setSubject(post.getContent()); 
    post.setCreated(new Date()); 
    post.setLatitude(null); 
    post.setLongtitude(null); 
    post.setTemperature(null); 
    postService.savePost(post); 
    ModelAndView mav = new ModelAndView("mvchome"); 

    mav.addObject("postsList", post); 
    Post newpost = new Post(); 

    mav.addObject("post", newpost); 
    return mav; 

} 


@RequestMapping(value = "/getAll") 
//public String create(@Valid Post post, BindingResult bindingResult, Model model) { 
public ModelAndView getAll() { 
//  if (bindingResult.hasErrors()) { 
//   return "mvchome"; 
//  } 

    List<Post> postList=postService.findAllPosts(); 
    ModelAndView mav = new ModelAndView("mvchome"); 
    mav.addObject("postsList", postList); 
    return mav; 

} 

} 

Çevremde arama yaptım ve şu ana kadar benzer bir işlevi bulamıyorum. Herhangi biri bana doğru yönde işaret edebilir mi? Takip

Teşekkür

**** şimdi "fasulye adı için ne BindingResult ne de düz hedef nesneye" benim hata için bu hatayı

23:50:25.592 [http-nio-8443-exec-10] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8443-exec-10] Exception processing template "mvchome": Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (mvchome:44) 
23:50:25.593 [http-nio-8443-exec-10] ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (mvchome:44)] with root cause 
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'post' available as request attribute 
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
+1

Kodunuz, soru :) 'cevabı olduğunu yaptık Show Wiki List' – sanluck

+0

Ben daha önce, ancak Eh bir hata – user2184653

+0

alıyorum, bu ihtiyacın olanı söyler çalıştı 'BindingResult' eklemek için. Bu yüzden denetleyici yönteminizi "public ModelAndView" (@Valid Post post, BindingResult sonucu) 'na dönüştürün ' – sanluck

cevap

0

alıyorum ancak önerildiği gibi konu oldu formum bir post nesnesini bekliyordu, ancak getAll yönteminde yeni bir post nesnesi sağlamadım. Burada hatayı gidermek için ben

@RequestMapping(value = "/getAll") 
// public String create(@Valid Post post, BindingResult bindingResult, Model 
// model) { 
public ModelAndView getAll() { 
    // if (bindingResult.hasErrors()) { 
    // return "mvchome"; 
    // } 

    List<Post> postList = postService.findAllPosts(); 
    ModelAndView mav = new ModelAndView("mvchome"); 
    mav.addObject("postsList", postList); 
    Post newpost = new Post(); 

    mav.addObject("post", newpost); 
    return mav; 

}