2012-12-23 46 views
0

我想在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(值= “/家庭”) 公共类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"; 
} 

的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> 

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()方法未被调用,因为sysouts在控制台中不会引发任何操作。如果我将home.jsp设置为应用程序的默认页面,则调用该方法,但不通过给定的方法。任何人都可以评论这个吗?

+0

你有ViewResolver的设置是否正确? –

+0

这部分看起来像一个错误:'value =“$ {category}”'您的代码中没有定义类别。 – Yevgeniy

+0

ViewResolver应该没问题,因为页面正常渲染。我甚至从jsp页面中删除了复选框代码,但仍然没有调用showForm()。 – user746458

回答

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> 
+0

我试过了,但现在链接正在将用户带到相同的welcome.jsp页面。我无法去home.jsp。我在代码中添加了web.xml和app-config.xml。 – user746458

+0

我已经尝试了很多在论坛中给出的例子,每个例子都和我写的一样。那么点击链接后为什么我不去home.jsp?任何人都可以评论这个吗? – user746458