【发布时间】:2016-11-24 17:11:37
【问题描述】:
我的 bean 被注释了
@Component("Person")
@Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS)
我有一些 getter 和 setter,但我对 JSP 感兴趣的是
@Transient
private ArrayList<DummyProduct> products = new ArrayList<DummyProduct>();
public ArrayList<DummyProduct> getProducts() {
return products;
}
public void setProducts(ArrayList<DummyProduct> products) {
this.products = products;
}
然后在我的控制器中将产品添加到该列表中
DummyProduct prod = new DummyProduct(product);
this.person.getProducts().add(prod);
然后在我的 JSP 中我尝试了,但没有得到产品:
<table id="cart_table" border="1">
<tr>
<th>Product</th>
</tr>
<c:forEach var="prd" items="${sessionScope.Person.products}" >
<tr>
<td>${prd.productName}</td>
</tr>
</c:forEach>
</table>
我还使用了以下属性,以便将我的 bean 暴露给 jsps:<beans:property name="exposeContextBeansAsAttributes"
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="exposeContextBeansAsAttributes" value="true" />
编辑:控制器:
@org.springframework.stereotype.Controller
public class Controller {
@RequestMapping(value= "/addProduct", method = RequestMethod.POST)
public String addProduct(HttpServletRequest request, Map<String, Object> model, @RequestParam String product){
DummyProduct prod = new DummyProduct(product);
this.person.getProducts().add(prod);
return "loggedIn";
}
}
表格总是空的。我已经调试过,我看到在我的 person bean 中,填充了产品列表。
【问题讨论】:
-
您是否尝试过在没有
scessionScope之前访问 bean? -
@nfechner 我现在试过但没用 :(
-
我们可以看看你的控制器类吗?
-
@Georgesvanhoutte 完成 :)
-
不要使用
${sessionScope.Person.products}使用${Person.products}。
标签: spring jsp spring-mvc