2013-08-03 72 views
1

我在使用Apache Tiles的表單中遇到了一些麻煩。所以... 我有一個jsp,它適用於每一頁。這是一個小的搜索表單。我應該在控制器的每個方法中添加@ModelAttribute("search") SearchForm query, BindingResult result。如何避免這種情況?控制器方法中令人討厭的參數

JSP:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> 
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<c:if test="${empty newsList}"> 
    <h2>NO RESULTS</h2> 
    Please, search something else</c:if> 
<c:if test="${!empty newsList}"> 
    <table class="data"> 

     <c:forEach items="${newsList}" var="news"> 
      <table style="width: 100%; border-collapse: collapse;"> 
       <tbody> 
        <tr> 
         <td 
          style="letter-spacing: 0px; word-spacing: 0px; width: 220px; vertical-align: top;"><strong> 
           <h3><a href="${pageContext.request.contextPath}/news/${news.id}">${news.title}</a></h3> 
         </strong></td> 
         <td 
          style="vertical-align: top; letter-spacing: 0px; word-spacing: 0px;">&nbsp;&#124;&nbsp;&nbsp;<c:forEach 
           items="${news.tags}" var="tag"> 
           <small><a 
            href="${pageContext.request.contextPath}/tags/${tag.id}">${tag.title}</a></small> 
          </c:forEach><br></td> 
        </tr> 
       </tbody> 
      </table> 
     </c:forEach> 

    </table> 
</c:if> 

瓷磚:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%> 
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<c:if test="${empty newsList}"> 
    <h2>NO RESULTS</h2> 
    Please, search something else</c:if> 
<c:if test="${!empty newsList}"> 
    <table class="data"> 

     <c:forEach items="${newsList}" var="news"> 
      <table style="width: 100%; border-collapse: collapse;"> 
       <tbody> 
        <tr> 
         <td 
          style="letter-spacing: 0px; word-spacing: 0px; width: 220px; vertical-align: top;"><strong> 
           <h3><a href="${pageContext.request.contextPath}/news/${news.id}">${news.title}</a></h3> 
         </strong></td> 
         <td 
          style="vertical-align: top; letter-spacing: 0px; word-spacing: 0px;">&nbsp;&#124;&nbsp;&nbsp;<c:forEach 
           items="${news.tags}" var="tag"> 
           <small><a 
            href="${pageContext.request.contextPath}/tags/${tag.id}">${tag.title}</a></small> 
          </c:forEach><br></td> 
        </tr> 
       </tbody> 
      </table> 
     </c:forEach> 

    </table> 
</c:if> 

控制器:

@RequestMapping(value = "/search/", method = RequestMethod.GET) 
    public String redirectNullSearch(
      @ModelAttribute("search") SearchForm query, BindingResult result) { 

     return "redirect:/"; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public String addContactProcess(@ModelAttribute("news") News news, 
      BindingResult result) { 

     newsService.addNews(news); 

     return "redirect:/"; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.GET) 
    public String addNewsProcess(Map<String, Object> map, 
      @ModelAttribute("search") SearchForm query, BindingResult result) { 

     map.put("news", new News()); 
     return "add"; 
    } 

    @RequestMapping(value = "search/{name}", method = RequestMethod.GET) 
    public String getSearch(@PathVariable String name, Map<String, Object> map, 
      @ModelAttribute("search") SearchForm query, BindingResult result) { 

     if (name.equals(null)) { 
      return "redirect:/"; 
     } 
     map.put("query", name); 
     map.put("news", new News()); 
     map.put("newsList", newsService.searchAllWithDetail(name)); 
     return "search"; 
    } 

搜索表單類:

package net.babobka.blog.form; 

public class SearchForm { 
    private String name; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

}

回答

0

有jsp頁面上的搜索表單提交給只有一個方法/ URL。你不需要它的所有方法/網址。

+0

但控制檯說我「沒有搜索結果」「 – babobka

+0

我應該粘貼一些代碼。 – babobka

+0

請看我的代碼。 – babobka