java - EL表达式只显示在特定的JSP页面上

标签 java xml spring jsp model-view-controller

我正在制作一个网上商店项目,我正在尝试使用 EL 表达式来列出正在销售的产品的可用类别。 我正在使用:

<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 语法错误吗? 我可能错过了向 View 发送一些东西,也许是其他东西......

以下是其余相关代码:

spring-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";
    }


}

如果您能提供任何提示或帮助,我将不胜感激,提前谢谢您! :)

编辑 - 添加dispatcher-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>

最佳答案

如果您只有 SiteController.java 作为 Spring Controller ,那么从我的角度来看,最好的方法是在相应的包中创建新的 Controller 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("/products") 指的是类似 http://localhost:8080/products 的 URL并且返回“产品”将转发到products.jsp。

请注意重复代码,因为现在 SiteController 实际上执行相同的工作(用新属性填充模型)。

不要忘记遵循命名约定:Allproducts 应该是 AllProducts(我猜只是 Product),对于 DAO 来说也是如此。

关于java - EL表达式只显示在特定的JSP页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599344/

相关文章:

java - Android AES 解密字符串花费的时间太长

Spring批量停止工作

java - 通过 Mapstruct 重用装饰映射器

java - SpringBoot 集成测试 Sybase 和 Testcontainers

java - 根据recyclerview中的最后聊天记录显示用户

xml - 我怎样才能获得与艺术家关联的所有 musicbrainz id?

java - 将 Java 公历转换为字符串

javascript - 自定义搜索字段属性以自定义文本 "No Data"

java - 多部分/表单数据 Restful 请求

java - JspFragment.invoke 到底做了什么?