控制器方法中的烦人参数

时间:2013-08-03 14:27:11

标签: spring jsp tiles

我在使用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;
    }

}

1 个答案:

答案 0 :(得分:0)

将jsp页面上的搜索表单提交给一个方法/ url。所有方法/网址都不需要它。

相关问题