Pergunta

I am new to Java.
I created a project below with Java Dynamic Web - servlet and it works.

@WebServlet("/the_data/userdata/userData")
public class userData extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

        response.setContentType("text/html");

        //query
        String query = "select * from country";

        try {
            //Note -> ConLib is my connection class
            ResultSet rst = ConLib.connect().createStatement().executeQuery(query);

            response.getWriter().print("<table>");
            response.getWriter().print("<tr>");
            while(rst.next()) {
                response.getWriter().print("<tr>");
                response.getWriter().print("    <td>src/"+rst.getString("id")+"</td>");
                response.getWriter().print("    <td>"+rst.getString("countryName")+"</td>");
                response.getWriter().print("    <td>"+rst.getString("countryName")+"</td>");
                response.getWriter().print("</tr>");
            }
            response.getWriter().print("</tr>");
            response.getWriter().print("</table>");

        } 
        catch (SQLException e) {
            e.printStackTrace();
        }

    }
}



Writing an html page like above is really hard.
I decide to write new project (Spring MVC Project). I am converting above code into Spring MVC Framework. I have no idea, the code doesn't work. I just want to write the array in country_view.jsp file.

@RequestMapping(value = "/country", method = RequestMethod.GET)
public String pasar(Model model) throws SQLException {

    String theSQL = "select * from country";

    //Note -> ConLib is my connection class
    ResultSet rst = ConLib.connect().createStatement().executeQuery(theSQL);

    model.addAttribute(rst);

    return "country_view";
}

This is country_view.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Country</title>
</head>
<body>
    <h1>Country</h1>

    <% while(rst.next()) { %>
        <%= rst.getString("countryName") %>
    <% } %>
</body>
</html>

This is error page:

type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 10 in the jsp file: /WEB-INF/views/country_view.jsp
rst cannot be resolved
7: <body>
8: <h1>Country</h1>
9:  
10:  <% while(rst.next()) { %>
11:     <%= rst.getString("countryName") %>
12:  <% } %>
13: </body>


An error occurred at line: 11 in the jsp file: /WEB-INF/views/country_view.jsp
rst cannot be resolved
8: <h1>Country</h1>
9:  
10:  <% while(rst.next()) { %>
11:     <%= rst.getString("countryName") %>
12:  <% } %>
13: </body>
14: </html>


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:468)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Would you please help me?
And how to write an array in 'spring jsp view'?
Thank You...

Foi útil?

Solução

Add this to the JSP

 <% ResultSet rst = (ResultSet)request.getAttribute("resultSet");%>
 <%= rst.getString("resultSet") %>

Change your pasar method to

model.addAttribute("resultSet",rst);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top