2016-07-26 72 views
0

我正在製作一個網上商店項目,我試圖用EL表達式列出正在出售的產品的可用類別。 我使用:EL表達式僅在特定的JSP頁面上顯示

<div class="list-group"> 

<a href="#" class="list-group-item">Back</a> 

<c:forEach items="${products}" var ="product"> 
<a href="./${product.id}" class="list-group-item"> ${product.name}</a> 
</c:forEach> 
</div> 

然而,EL表達式被「產品」頁上的忽略,索引頁上不容忽視。 這可能是一個MVC語法錯誤? 我可能已經錯過了送東西來的觀點,或許其他什麼東西......

下面是相關代碼的其餘部分:

彈簧database.xml-

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 


    <bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="/WEB-INF/jdbc.properties" /> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.url}" 
    p:username="${jdbc.username}" 
    p:password="${jdbc.password}" /> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
     <list> 
      <value>com.mycompany.web_shop.model.Allproducts</value>  
     </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> 
       <prop key="hibernate.current_session_context_class">thread</prop> 
      </props> 
     </property> 
    </bean> 





    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --> 


</beans> 

applicationContext.xml -

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 


<mvc:annotation-driven/> 
<context:component-scan base-package="com.mycompany.web_shop.controller"/> 
<context:component-scan base-package="com.mycompany.web_shop.model"/> 
<mvc:resources mapping="/resources/**" location="/resources/"/> 

    <!--bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
      p:location="/WEB-INF/jdbc.properties" /> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.url}" 
    p:username="${jdbc.username}" 
    p:password="${jdbc.password}" /--> 

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --> 

</beans> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    version="3.1"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml, 
      /WEB-INF/spring-database.xml 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
</web-app> 

AllproductsDao -

package com.mycompany.web_shop.model; 

import java.util.List; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

//@Transactional(propagation = Propagation.REQUIRED, readOnly = false) 
@Component 
public class AllproductsDao { 

    @Autowired 
    SessionFactory sessionFactory; 

    public List<Allproducts> find() { 
     Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 
     List<Allproducts> result = session.createCriteria(Allproducts.class).list(); 
     session.getTransaction().commit(); 
     return result; 
    } 
} 

SiteController -

package com.mycompany.web_shop.controller; 


import com.mycompany.web_shop.model.Allproducts; 
import com.mycompany.web_shop.model.AllproductsDao; 
import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.ui.ModelMap; 

@Controller 
public class SiteController { 


    @Autowired 
    AllproductsDao allproductsDao; 


    @RequestMapping("/") 
    public String index(ModelMap model) { 

     List<Allproducts> products = allproductsDao.find(); 

     model.addAttribute("products", products); 

     return "index"; 
    } 


} 

我會感激任何一種小費或幫助,提前謝謝! :)

編輯 - 添加調度-servlet.xml中

< 

?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?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:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 



    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" 
      p:suffix=".jsp" /> 

</beans> 

回答

1

如果只有SiteController.java作爲一個春天控制器,然後從我的觀點來看,最好的方法是創建新在相應的封裝控制器ProductsController.java:

package com.mycompany.web_shop.controller; 
@Controller 
public class ProductsController { 

    @Autowired 
    AllproductsDao allproductsDao; 

    @RequestMapping("/products") 
    public String listProducts(ModelMap model) { 
    List<Allproducts> products = allproductsDao.find(); 
    model.addAttribute("products", products); 
    return "products"; 
    } 
} 

@RequestMapping( 「/產品」)指URL等http://localhost:8080/products返回「products」將轉發到products.jsp。

請注意重複的代碼,因爲現在SiteController實際上做了相同的工作(用新屬性填充模型)。

不要忘記遵循命名約定:所有產品應該是AllProducts(我猜就是產品),對於DAO來說也是一樣的。