从控制器传递后,无法在jsp中显示列表

时间:2014-06-12 16:07:46

标签: hibernate jsp spring-mvc

我正在尝试使用JSTL标记在JSP中显示list的值。但没有显示任何内容。我找不到问题。这是我在JSP中的代码

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

the length is ${fn:length(test1)}
 <c:forEach var="ex" items="${test1}">
   <c:out value="${ex.userid}" />
   <c:out value="${ex.password}" />
</c:forEach>

这是我的控制器代码

@RequestMapping("/login.htm")
   public ModelAndView login(HttpServletRequest request,HttpServletResponse response) throws Exception
    {  
        ModelMap modelMap = new ModelMap();
        List test=registrationDAO.getLoginInfo();
        modelMap.addAttribute("test1",test);
        return new ModelAndView("login",modelMap);
    }

这是我的DAO代码

     package registration;


    import java.util.List;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;

    @Repository("registrationDAO")
    public class registrationDAOImpl implements registrationDAO{

        @Autowired 
        public static SessionFactory getSessionFactory()
        {
            Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            SessionFactory factory = configuration.buildSessionFactory(builder.build());
            return factory;
        }
@Override
    public List<Login> getLoginInfo() {
        Session session=getSessionFactory().openSession();
        Query query = session.createQuery("from Login");
        List test = query.list();
        return test;
    }

}

这是我的登录类

package registration;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "login")

public class Login {

    @Id
    @Basic(optional = false)
    @Column(name = "userid")
    private String userid;
    @Column(name = "password")
    private String password;
    @Column(name = "role")
    private String role;

    public Login() {
    }

    public Login(String userid) {
        this.userid = userid;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }


}

这是web.xml代码。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

2 个答案:

答案 0 :(得分:0)

使用:

    {  
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<Login> test=registrationDAO.getLoginInfo();

        modelMap.put("test1",test);

        return new ModelAndView("login",modelMap);
    }

答案 1 :(得分:0)

问题得到了解决。这是模型和视图类的导入问题。而不是导入导入org.springframework.web.servlet.ModelAndView;

我的控制器从导入org.springframework.web.portlet.ModelAndView导入它;

现在显示长度并正确显示数据