2013-02-13 4 views
6

Hallo kann jemand bitte helfen, warum dies einen Fehler gibt, wenn versucht wird, die Werte in einer JSP-Seite anzuzeigen. Ich habe nicht eine beliebige Zahl umgewandelt oder String Zahl umgewandelt worden ist, jedoch bekomme ich Numberjava.lang.NumberFormatException: Für Eingabezeichenfolge in JSP Seite

meine Servlets die Anfrage bekommen

if(action.equalsIgnoreCase("update")){ 
     System.out.println("Came into Update"); 
     userId=(int) Integer.parseInt(request.getParameter("userid")); 
     nbId=request.getParameter("nbId").trim(); 
     System.out.println("User iD and NBid: "+ userId + nbId); 

     User user=new User(); 
     user.setUser_id(userId); 
     user.setUser_nbk(nbId); 



     List userRecords=UserDAO.getUserRecord(user); 
     request.setAttribute("userRecords", userRecords); 
     List owningOrg=Owning_Org_DB.getOwningOrgRecords(); 
     request.setAttribute("owningOrg", owningOrg); 
     request.getRequestDispatcher("WEB-INF/JSP/TableMaintenance/UserNewAdd.jsp").forward(request, response); 

POJO einen Benutzerdatensatz anzuzeigen:

@Entity 
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE) 
    public class User { 

@Id 
private int user_id; 
private String user_first_name; 
private String user_middle_name; 
private String user_last_name; 

     //getters and setters methos 
    } 

DAO:

public static List getUserRecord(User obj){ 

    Logger lo=LoggerFactory.getLogger("UserDAO.getUserRecord"); 
    Session session= Annotationsessionfactory.getAnnotationSession(); 
    Transaction tx=session.beginTransaction(); 

    lo.debug("Request for A user Record"); 
    //List<User> recList=new ArrayList<User>(); 
    List recList=null; 
    try{ 

     //String userRecord="from User"; 
     Criteria userList=session.createCriteria(User.class) 
       .add(Restrictions.eq("user_nbk", obj.getUser_nbk())) 
       .add(Restrictions.eq("user_id", obj.getUser_id())); 



     recList=userList.list(); 
     System.out.println(recList.size()); 

    }catch (Exception e){ 

     lo.info("Exception Occured in UserDAO.getUserRecord"); 
     lo.debug("Exception Occured in UserDAO.getUserRecord:"+e); 
     tx.rollback(); 

    }finally{ 

     session.close(); 

     lo.info("Session Closed in UserDAO.userRecordslist finally block: "); 
    } 

    lo.debug("Record was sent to the requesting servlet or method: "+ recList); 
    return recList; 

} 

JSP Seite

<table> 
<tr> 
    <td>First Name</td> 
    <td><input type="text" id="firstname" name="firstname" maxlength="80" value="">${userRecords.user_first_name}</td> 
</tr> 
<tr> 
<td>Middle Name</td> 
<td><input type="text" id="middlename" name="middlename" maxlength="80" value="${userRecords.user_middle_name}"></td> 

....... 

Ausnahme erhalte ich:

SEVERE: Servlet.service() for servlet jsp threw exception 
**java.lang.NumberFormatException: For input string: "user_first_name"** 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
at java.lang.Integer.parseInt(Integer.java:447) 
at java.lang.Integer.parseInt(Integer.java:497) 
at javax.el.ListELResolver.coerce(ListELResolver.java:166) 
at javax.el.ListELResolver.getValue(ListELResolver.java:51) 
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54) 
at org.apache.el.parser.AstValue.getValue(AstValue.java:123) 
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) 
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938) 
at org.apache.jsp.WEB_002dINF.JSP.TableMaintenance.UserNewAdd_jsp._jspService (UserNewAdd_jsp.java:77) 
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) 
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) 
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646) 
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436) 
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374) 
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302) 
at com.servicedbUpdate.UserUpdateDB_NewAdds.doPost(UserUpdateDB_NewAdds.java:79) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879) 
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600) 
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703) 
at java.lang.Thread.run(Thread.java:619) 
    Feb 13, 2013 10:30:49 AM org.apache.catalina.core.StandardWrapperValve invoke 
    SEVERE: Servlet.service() for servlet UserUpdateDB_NewAdds threw exception 
     **java.lang.NumberFormatException: For input string: "user_first_name"** 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
at java.lang.Integer.parseInt(Integer.java:447) 
at java.lang.Integer.parseInt(Integer.java:497) 
at javax.el.ListELResolver.coerce(ListELResolver.java:166) 
at javax.el.ListELResolver.getValue(ListELResolver.java:51) 
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54) 
at org.apache.el.parser.AstValue.getValue(AstValue.java:123) 
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) 
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:938) 
at org.apache.jsp.WEB_002dINF.JSP.TableMaintenance.UserNewAdd_jsp._jspService(UserNewAdd_jsp.java:77) 
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) 
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) 
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646) 
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436) 
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374) 
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302) 
at com.servicedbUpdate.UserUpdateDB_NewAdds.doPost(UserUpdateDB_NewAdds.java:79) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879) 
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600) 
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703) 
at java.lang.Thread.run(Thread.java:619) 
+3

'Integer.parseInt (request.getParameter (" Benutzer-ID "))' ist in Ihrem Code. – jlordo

+0

aber das gibt mir keinen Fehler .. ich konnte es in Benutzerobjekt ohne Fehler platzieren und Hibernate hat den Datensatz (ich überprüfte die Größe der Liste) gezogen. Fehler tritt auf, wenn die Anfrage in die JSP-Seite geht. – VBJ

+1

userRecords ist eine Liste von Benutzern. Sie müssen die userRecords durchlaufen, um zu jedem Benutzer zu gelangen – orangegoat

Antwort

18

Die ${userRecords} hier

${userRecords.user_first_name} 
${userRecords.user_middle_name} 

ein List<User> ist aber bist versuchen Sie es zugreifen, als ob es eine einzige User ist. Dies ist nicht gültig. In EL kann ein List nur mit einem ganzzahligen Index zugegriffen werden, um die Position der Listenposition anzeigt, dass Sie, wie so

${userRecords[0].user_first_name} 
${userRecords[0].user_middle_name} 

Die Ausnahme auch, dass es einen ganzzahligen Wert im Grunde sagt zugreifen mögen erwartet statt eines Zeichenfolgewerts. Wenn Sie sich den Stack-Trace näher ansehen, sehen Sie, dass ein ListELResolver beteiligt ist.

Ihr konkretes Problem ist jedoch größer. Sie sollten tatsächlich über die Liste iterieren. Verwenden Sie dazu die JSTL<c:forEach>. Z.B. (Vereinfacht aus dem ungeraden Codeausschnitt):

<table> 
    <c:forEach items="${userRecords}" var="user"> 
     <tr> 
      <td>${user.user_first_name}</td> 
      <td>${user.user_middle_name}</td> 
     </tr> 
    </c:forEach> 
</table> 

(beachten Sie: Halten Sie die XSS attack hole im Auge, wenn Sie wirklich beabsichtigen, sie als Eingabewerte erneut anzuzeigen)

By the way, würde ich die Arbeit an Ihre Java code conventions. Diese Unterstriche sind wirklich nicht Java-ish.