Spring MVC Controller model.addAttribute

时间:2014-03-22 07:32:01

标签: spring-mvc spring-jdbc

我是Java新手 我使用Java Dynamic Web - servlet在下面创建了一个项目,它可以工作。

@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();
        }

    }
}

写上面这样的html页面真的很难。
我决定编写新项目(Spring MVC Project)。我正在将上面的代码转换为Spring MVC Framework。 我不知道,代码不起作用。我只想在country_view.jsp文件中编写数组。

@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";
}

这是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>

这是错误页面:

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)

请你帮我吗?
如何在'spring jsp view'中写一个数组? 谢谢......

1 个答案:

答案 0 :(得分:1)

将其添加到JSP

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

将您的pasar方法更改为

model.addAttribute("resultSet",rst);