[英]EL expression shows on specific JSP pages only
I am making a web shop project, and I'm trying to use EL expressions to list out the available categories of products which are being sold. 我正在做一个网上商店项目,并且试图使用EL表达式列出要出售的产品的可用类别。 I'm using:
我正在使用:
<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>
However, the EL expression is being ignored on the "products" page, and not ignored on the index page. 但是,EL表达式在“产品”页面上被忽略,而在索引页面上则未被忽略。 Could this be a MVC error in syntax?
难道这是MVC语法错误? I could have missed to send something to the view, perhaps something else...
我可能会错过向视图发送某些内容的机会,也许还有其他...
Here is rest of the relevant code: 以下是相关代码的其余部分:
spring-database.xml- 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 - 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 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 - 所有产品Dao-
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 - 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";
}
}
I would be grateful for any kind of tip or help, thank you in advance! 我将不胜感激任何提示或帮助,在此先感谢您! :)
:)
EDIT - Adding dispatcher-servlet.xml 编辑-添加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>
If you have only SiteController.java as a Spring controller, then the best way from my point of view is to create new controller ProductsController.java in corresponding package: 如果您只有SiteController.java作为Spring控制器,那么从我的角度来看,最好的方法是在相应的包中创建新的控制器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";
}
}
Where @RequestMapping("/products") refers to URL like http://localhost:8080/products and return "products" will forward to products.jsp. @RequestMapping(“ / products”)指的是URL,例如http:// localhost:8080 / products , 返回“ products”将转发到products.jsp。
Be aware of duplicate code, because now SiteController do the same work actually (filling model with new attribute). 请注意重复的代码,因为现在SiteController实际上会执行相同的工作(使用new属性填充模型)。
Don't forget to follow naming conventions: Allproducts should be AllProducts (and I guess simply Product), the same for the DAO. 不要忘了遵循命名约定:Allproducts应该是AllProducts(我想只是Product),对于DAO来说也是一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.