Writing java codes in jsp(Spring Mvc)

2019-08-19 08:06发布

问题:

I am new to spring MVC. I need to write java codes in a jsp(though its not a good practice, but i don't have other option). My Jsp is now like

          <c:forEach items="${dbEntries}" var="c" varStatus="loop">
                        <tr> 
                              <% 
                                  int i = 0;
                                  system.out.println(i);
                              %>
                              <td rowspan="1">${c.getRh_name()}</td>                                 
                              <td rowspan="1">${c.getIpm_name()}</td>` 
                        </tr>
          </c:forEach>

but its giving an error "org.apache.jasper.JasperException: Unable to compile class for JSP:". Can anyone help??

included headers are

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form"  prefix="spring"%>
<core:set var="contextPath" value="${pageContext.request.contextPath}"/>
<%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

full exception stacktrace Stacktrace: org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366) org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:490) org.apache.jasper.compiler.Compiler.compile(Compiler.java:379) org.apache.jasper.compiler.Compiler.compile(Compiler.java:354) org.apache.jasper.compiler.Compiler.compile(Compiler.java:341) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238) org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) javax.servlet.http.HttpServlet.service(HttpServlet.java:624) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

回答1:

Use jstl instead of java code inside jsp like this.

<c:set var="salary" scope="session" value="${2000*2}" /> 
 <c:out value="${salary}" /> 
<c:forEach begin="1" end="5" step="1">
     <c:set var="salary" value="${2000*2}" /> 
</c:forEach> <c:out value="${salary}" />


回答2:

<td>${loop.count}</td> will return you the count of iteration going on. Its like i+1 in for loop for(int i=0;i<10;i++);