Вопрос

in my out put jsp file details are not showing. I used cout tag. Please advise

student.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@  taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01      Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
       <title>Insert title here</title>
   </head>
   <body>
   <c:if test="${not empty objects}">
   <table>
    <tr>
     <td>ID</td>
          <td>Name</td>
          <td>Age</td>
    </tr>
    <c:forEach var="o" items="${objects}">
        <tr>
            <td><c:out value="${o.id}"/></td>
            <td><c:out value="${o.name}"/></td>
            <td><c:out value="${o.age}"/></td>   
        </tr>
    </c:forEach>

    </table>
  </c:if>  

</body>
</html>

Controller function i used

@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView students() {

    List<Student> students=studentService.getAllStudents();
    return new ModelAndView("students", "command", students);
}

getAllstudents function in Service

    public List<Student> getAllStudents()
    {
        List<Student> students=new ArrayList<Student>();

        for(int i=0;i<10;i++)
        {
            Student student=new Student();
            student.setName("Student - " + i);
            student.setId(i);
            student.setAge(i+20);
            students.add(student);
        }

        return students;
    }
Это было полезно?

Решение

1.Add <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

I am not sure whether your code works or not but if you replace below code it works fine

2.In Controller class change the code modify

ModelAndView view=new ModelAndView();
    view.addObject("stdobj", students);
    view.setViewName("students");
    return view;

3.change the code like this

  <c:forEach var="o" items="${stdobj}">
    <tr>
        <td><c:out value="${o.id}"/></td>
        <td><c:out value="${o.name}"/></td>
        <td><c:out value="${o.age}"/></td>   
    </tr>
</c:forEach>

Другие советы

You have the very basic setup problem. First put this below line at the top

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Add the JSTL library to the classpath (WEB-INF/lib)

Note : Download link for JSTL 1.2 library

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top