复选框列表未出现在Spring MVC注释中

时间:2012-12-23 19:04:40

标签: spring spring-mvc

我正在尝试使用注释在Spring MVC中实现动态复选框列表。所以,我尝试了这个:

在home.jsp中:

             <c:forEach items="${categories}" var="cat">
                <tr>
                    <td><form:checkbox path="catrgo" value="${category}"
                            label="${cat.categoryId}" /></td>
                    <td><c:out value="${cat.category}" /></td>
                </tr>
            </c:forEach>

在HomeController中:            @Controller             @RequestMapping(value =“/ home”)            公共课HomeController {

@RequestMapping(method = RequestMethod.GET)
public String showForm() {
    FeedDomain feedDomain = new FeedDomain();

    System.out.println("Called");
    CategoryService categoryService = new CategoryService();
    try {
        List<CategoryDomain> categoryDomainList = new   ArrayList<CategoryDomain>();
        CategoryDomain categoryDomain = new CategoryDomain();
        categoryDomain.setCategory("aaa");
        categoryDomain.setCategoryId(11111);

        System.out.println("Size is " + categoryDomainList.size());
    } catch (Exception exception) {

    }
    return "home";
}

现在我将使用标签从index.jsp转到home.jsp,但问题是showForm方法没有被调用。可能是什么原因?

我发布在完整的流程之下:

这是来自我调用home.jsp

的welcome.jsp
       <body>
             <a href="home.jsp" class="btn btn-success btn-large disabled">Get
                        Us Feeds Now</a>
       </body>

现在我简化了正在渲染的home.jsp:

    <body>

<br /> &nbsp;&nbsp;
<span class="badge badge-info"><h1>Chat Booster</h1></span>
<div class="leftHeader">
    <h1>
        <small>Subtext for header</small>
    </h1>
</div>
<br></br>
<br></br>

    </body>

主页控制器是:

         @Controller
        @RequestMapping(value = "/home")
    public class HomeController {

@RequestMapping(method = RequestMethod.GET)
public String showForm() {
    FeedDomain feedDomain = new FeedDomain();
//  model.addAttribute("feedDomain", feedDomain);
    System.out.println("Called");
    CategoryService categoryService = new CategoryService();

    return "home";
}

的web.xml:

             <?xml version="1.0" encoding="UTF-8"?>
     <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>HelloWorldExampleWithSpring3MVCInEclipse</display-name>
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-  class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/app-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
    </web-app>

应用-config.xml中:

         <?xml version="1.0" encoding="UTF-8"?>
     <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.controller" />

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views 
    directory -->
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="" />
    <property name="suffix" value=".jsp" />
</bean>
    </beans>

我的jsp页面只在webcontent文件夹中(不在web-inf中)。现在我将在稍后查找主要问题,但第一点是showForm()方法未被调用,因为sysout是在控制台上没有任何东西。如果我将home.jsp作为应用程序的默认页面,则调用该方法,但不是通过给定的方法。有人可以对此发表评论吗?

1 个答案:

答案 0 :(得分:0)

您的链接不正确,您需要调用控制器操作而不是jsp:

   <body>
       <c:url var="homeLink" value="/home"/>
       <a href="${homeLink}" class="btn btn-success btn-large disabled">Get Us Feeds Now</a>
   </body>