如何从JSP表单数据查询数据库?

时间:2014-04-30 01:48:14

标签: java spring hibernate spring-mvc

我需要帮助。我正在尝试编写一个填充下拉列表的基本表单,并根据您选择的内容查询数据库。但是,我无法做到这一点,它只是收到所有的查询。我很茫然如果您有任何有用的建议或有用的链接,那就太棒了。我真的需要学习Spring MVC和Hibernate的结合。谢谢!

HomeController代码:(此控制器填充列表)

/**
 * Handles requests for the application home page.
 */

@Controller
public class SearchController {

@Autowired
FilmBo filmBo;

/**
 * Simply selects the home view to render by returning its name.
 */

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, ModelMap model) {

    Selection selection = new Selection();
    model.addAttribute("selection", selection);
    return "search";
}



@ModelAttribute("categoryList")
public List<String> populateList(){
    List<String> categoryList = filmBo.searchByCategory("");
    return categoryList;
}

}

以下是将提交的表单发送到的主控制器:

public class FilmController {
private static final Logger logger =           LoggerFactory.getLogger(SearchController.class);

@Autowired
FilmBo filmBo;



@RequestMapping(value = "/films", method = RequestMethod.GET)
public String home(Locale locale, Model model,      @RequestParam(value="title",required=false) String title,
        @RequestParam(value="category",required=false) String category) {
    logger.info("Welcome home! The client locale is {}.", locale);
    List<Film> filmList = filmBo.searchByTitle(title);
    logger.info("filmList: " + filmList);
    model.addAttribute("filmList", filmList);

    List<String> filmCategory = filmBo.searchByCategory(category);
    logger.info("filmCategory: " + filmCategory);
    model.addAttribute("filmCategory",filmCategory);

    return "films";
}
}

这是我的jsp页面:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ page session="false"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<div class="form">
    <form:form action="${pageContext.request.contextPath}/films"
        method="GET" commandName="selection">
        Film Title<input type="text" name="title">
        <br>
        Select a Film Category:<br>
        <form:select path="selection">
            <form:option value="NONE" label="--- Select ---" />
            <form:options name="category" items="${categoryList}" />
        </form:select>

        <button name="submit" type="submit">Submit Name</button>
    </form:form>
</div>
</body>
</html>

0 个答案:

没有答案
相关问题