Spring MVC显示来自数据库的数据

时间:2016-01-21 06:25:44

标签: java spring jsp spring-mvc

我正在尝试显示数据库中的数据,但我收到了错误

LoginSuccess.java

package java4s;


import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java4s.EmployeeService;

@Controller
@Scope("session")
public class LoginSuccessController {

    @Autowired
    EmployeeService emp_service;

    @RequestMapping(value = "/login", method=RequestMethod.POST)
    public ModelAndView loginvalidateForm(ModelMap model, @ModelAttribute("loginForm") Employee employee, HttpSession session) {


        if(emp_service.validateLogin(employee.getUsername(), employee.getPassword()))
        {
            List<Employee> user_info = emp_service.getUserinfo(employee.getUsername());
            session.setAttribute("session_username", employee.getUsername()); //Add value to session variable
            model.addAttribute("result", "Login Success");
            model.addAttribute("user_info", user_info);
        }
        else
            model.addAttribute("result", "Login Failure");
        return new ModelAndView("LoginSuccess",model);

    }
}

LoginSuccess.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login Result</title>
</head>
<body>
${result}
<c:if test="${session_username != null }">Hello ${session_username}!
<div align="center" style="top:100px;">
<H1>My Information</H1>
<TABLE>
    <TR>
        <TD>Username :</TD>
        <TD>${$user_info.username}</TD>
    </TR>
    <TR>
        <TD>Firstname :</TD>
        <TD>${$user_info.firstname}</TD>
    </TR>
</TABLE>
</div>
</c:if>
</body>
</html>

EmployeeServiceImpl.java

package java4s;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import java4s.Employee;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;

public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    DataSource dataSource;

    @Override
    public List<Employee> getUserinfo(String username) {
        // TODO Auto-generated method stub

        String sql_query = "SELECT * FROM EmployeeInfo WHERE username = '" + username + "'";
        List<Employee> user_info = new ArrayList<Employee>();

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        user_info = jdbcTemplate.query(sql_query, new RowMapper<Employee>(){

            @Override
            public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
                Employee employee = new Employee();

                employee.setUsername(rs.getString("username"));
                employee.setFirstname(rs.getString("firstname"));

                return employee;
            }
        });
        return user_info;
    }
}

错误

org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/LoginSuccess.jsp at line 18

15: <TABLE>
16:     <TR>
17:         <TD>Username :</TD>
18:         <TD>${user_info.username}</TD>
19:     </TR>
20:     <TR>
21:         <TD>Firstname :</TD>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    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:264)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.NumberFormatException: For input string: "username"
    java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    java.lang.Integer.parseInt(Integer.java:492)
    java.lang.Integer.parseInt(Integer.java:527)
    javax.el.ListELResolver.coerce(ListELResolver.java:173)
    javax.el.ListELResolver.getValue(ListELResolver.java:52)
    org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
    org.apache.el.parser.AstValue.getValue(AstValue.java:183)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
    org.apache.jsp.jsp.LoginSuccess_jsp._jspx_meth_c_005fif_005f0(LoginSuccess_jsp.java:125)
    org.apache.jsp.jsp.LoginSuccess_jsp._jspService(LoginSuccess_jsp.java:85)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    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:264)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

1 个答案:

答案 0 :(得分:1)

在jsp中试试这个。

<c:forEach items="${user_info}" var="user_info">
    <c:out value="${user_info.username}" />
</c:forEach>