0

我正在尝试在带有注释的 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") public class 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 方法没有被调用。可能是什么原因?

我在完整流程下方发布:

这是welcome.jsp,我从这里调用home.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";
}

网页.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>

应用程序配置.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() 方法没有被调用,因为 sysouts 没有在控制台上打印任何东西。如果我将 home.jsp 作为应用程序的默认页面,则调用该方法,但不是通过给定的方法。任何人都可以对此发表评论吗?

4

1 回答 1

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>
于 2012-12-23T20:53:28.430 回答