我正在使用Spring MVC。我正在制作一个虚拟项目,我使用hibernate从数据库中获取衣服类别,并将其添加到modelAndView(添加为列表)属性并将其发送到视图页面。在视图页面中,我必须显示类别。但我无法获得该值。 请在查看页面时调用我的数字格式异常。
这是我的控制器代码。
@RequestMapping(value = "/insproduct")
public ModelAndView insertProduct(HttpServletResponse response)
throws IOException {
List<Catogories> cat = categories.selectCategories();
ModelAndView mav = new ModelAndView("admin/insproduct");
mav.addObject("cato", cat);
return mav;
}
Controller调用我的Jsp页面。这是我的jsp页面。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="online.model.Catogories" %>
<anyxmlelement xmlns:c="http://java.sun.com/jsp/jstl/core">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<div style="text-align: center;">
<div style="box-sizing: border-box; display: inline-block; width: auto; max-width: 480px; background-color: #FFFFFF; border: 2px solid #0361A8; border-radius: 5px; box-shadow: 0px 0px 8px #0361A8; margin: 50px auto auto;">
<div style="background: #0361A8; border-radius: 5px 5px 0px 0px; padding: 15px;"><span style="font-family: verdana,arial; color: #D4D4D4; font-size: 1.00em; font-weight:bold;">ENTER THE PRODUCT</span></div>
<div style="background: ; padding: 15px">
<style type="text/css" scoped>
<select style="background: ; padding: 15px">
<select style="text-align: left;">
td { text-align:left; font-family: verdana,arial; color: #064073; font-size: 1.00em; }
input { border: 1px solid #CCCCCC; border-radius: 5px; color: #666666; display: inline-block; font-size: 1.00em; padding: 5px; width: 100%; }
input[type="button"], input[type="reset"], input[type="submit"] { height: auto; width: auto; cursor: pointer; box-shadow: 0px 0px 5px #0361A8; float: right; margin-top: 10px; }
table.center { margin-left:auto; margin-right:auto; }
.error { font-family: verdana,arial; color: #D41313; font-size: 1.00em; }
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="input" action="demo_form_action.asp" method="get">
<br>
PRODUCT ID: <input type="text" name="product_id"><br>
PRODUCT NAME: <input type="text" name="product_name"><br>
PRODUCT BUY PRICE: <input type="text" name="product_buy_price"><br>
PRODUCT SELL PRICE: <input type="text" name="product_sell_price"><br>
<c:forEach items="${cat}" var="cato">
<tr>
<td>${cato.getCatogories_name}</td>
</tr>
</c:forEach>
<br>
<input type="radio" name="sex" value="male">Male
<input type="radio" name="sex" value="female">Female
<br>
<input type="radio" name="age_group" value="adult">adult
<input type="radio" name="age_group" value="child">child
<br>
PRODUCT SIZE: <input type="text" name="product_detail_size"><br>
PRODUCT COLOR: <input type="text" name="pproduct_image"><br>
PRODUCT DISCOUNT: <input type="text" name="product_discount"><br>
<input type="button" value="ENTER" align="right" width="48" height="48">
</form>
</body>
</html>
这是我的POJO课程,我从中获得了价值观。请帮帮我。
package online.model;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="product_catogories")
public class Catogories {
@Id
private int catogories_id;
@Column(name="catogories_name")
private String catogories_name;
@ManyToMany(mappedBy = "catogories")
private Set<Product> products;
public Set<Product> getProducts() {
return products;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
public int getCatogories_id() {
return catogories_id;
}
public void setCatogories_id(int catogories_id) {
this.catogories_id = catogories_id;
}
public String getCatogories_name() {
return catogories_name;
}
public void setCatogories_name(String catogories_name) {
this.catogories_name = catogories_name;
}
}
这是我的DAO课程实施
package online.deo;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import online.model.Catogories;
import online.services.CategoriesServices;
@Transactional
@Repository
@Component
public class CategoriesServicesImpls implements CategoriesServices {
CategoriesServicesImpls() {
}
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public int insertCategories() {
return 0;
}
@Override
public int updateCategories() {
return 0;
}
@Override
public int deleteCategories() {
return 0;
}
@Override
@Transactional(readOnly = true)
public List<Catogories> selectCategories() {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from Catogories");
return query.list();
}
}
这就是我得到的错误。我试图在错误下,但它只是给我有数字格式异常。我不知道它是给出错误
Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "getCatogories_name"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at javax.el.ListELResolver.coerce(ListELResolver.java:163)
at javax.el.ListELResolver.getValue(ListELResolver.java:51)
at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
at org.apache.jsp.WEB_002dINF.jsp.admin.insproduct_jsp._jspService(insproduct_jsp.java:104)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1157)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:927)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
感谢您的帮助。我也对在jstl中使用foreach循环感到困惑。
答案 0 :(得分:1)
由于cat是列表的集合,当你执行${cato.getCatogories_name}
时,表达式语言会尝试将getCatogories_name作为一个数字(列表中的索引,即cato [])进行操作,因此它会尝试将getCatogories_name名称解析为数字,你最终得到了NumberFormatException。
如何使用c:out?
迭代列表<c:forEach items="${cato}" var="cat" varStatus="status">
<c:out value="${cato[status.index].catogories_name}" />
</c:forEach>
答案 1 :(得分:0)
请求范围中的变量名称为cato
:
mav.addObject("cato", cat);
在cat
循环
forEach
<c:forEach items="${cat}"
另外,根据Unified Expression Language,您必须使用语法bean.propertyName
而不是bean.methodName
来访问bean的属性
因此,请尝试使用此代码,看看它是否解决了问题:
<c:forEach items="${cato}" var="cat">
<tr>
<td>${cat.catogories_name}</td>
</tr>
</c:forEach>
最后出错的原因是:
在请求范围cato
的类型为java.util.List
,因此您可以使用cato.indexValue
或cato[indexValue]
indexValue
来访问给定索引处的元素(应该是数字)是一个数字。
因此,JSP标记假设您正在传递索引,并尝试将getCatogories_name
转换为数字,然后面临NumberFormatException
。有关详细信息,请参阅此链接:Custom list + JSP + java.lang.NumberFormatException
答案 2 :(得分:0)
使用JSTL
迭代您的对象时。您可以直接访问其字段,并确保您的modelandview中的密钥与您的items
或EL
你在这里放了cat
对象cato
mav.addObject("cato", cat);
并使用我认为不存在的${cat}
。
<c:forEach items="${cat}" var="cato">
<tr>
// you are calling the method but you can call the field directly
<td>${cato.getCatogories_name}</td>
</tr>
</c:forEach>
更改为
<c:forEach items="${cato}" var="cato">
<tr>
<td>${cato.catogories_name}</td>
</tr>
</c:forEach>
答案 3 :(得分:0)
您可以尝试使用jsp:
<c:forEach items="${cato}" var="c">
<tr><td>${c.catogories_name}</td></tr>
</c:forEach>