show query result Spring java in the view

2

Vista:

<body>
    <p>Hola bienvenido</p>
    <c:forEach items="${datos}" var="dato">
        <c:out value="${dato.ID}"/>
    </c:forEach>
</body>

Connector: (the connection works)

public DriverManagerDataSource conectar(){
    DriverManagerDataSource dm=new DriverManagerDataSource();
    dm.setDriverClassName("com.mysql.jdbc.Driver");
    dm.setUrl("jdbc:mysql://localhost/gameunity");
    dm.setUsername("root");
    dm.setPassword("");
    return dm;
}

Driver:

@Controller
public class MainController {
    private JdbcTemplate template;

    public MainController(){
        Conector con=new Conector();
        this.template=new JdbcTemplate(con.conectar());   
    }

    @RequestMapping("index.htm")
    public ModelAndView index(){
        ModelAndView res=new ModelAndView();
        String consulta="select * from cliente";
        List datos    =this.template.queryForList(consulta);
        res.addObject("datos", datos);
        res.setViewName("index");
        return res;
    }
}

I'm not able to show data from my database in my view, what is my error?

I have simplified the code in order to pass a simple attribute from the controller, but I still can not show it in the view.

Vista:

<body>
    <p>Hola bienvenido</p>
        <c:out value="${datos}"/>
</body>

Driver:

@Controller
public class MainController {


    @RequestMapping("index.htm")
    public ModelAndView index(){
        ModelAndView res=new ModelAndView();
//        String consulta="select * from cliente";
//        List datos    =this.template.queryForList(consulta);
//        ArrayList<String> paraEnviar=new ArrayList();
        String dato="hola";
        res.addObject("dato",dato);
        res.setViewName("index");
        return res;
    }       
}

I can not show the value of the variable datoAux in the view, what is my error? edit: increasing information:

  

dispachet-servet

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">MainController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="MainController"
          class="controller.MainController"/>
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>
  

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>2</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>
    
asked by hubman 01.04.2017 в 05:35
source

4 answers

3

One of the things that would do would be to create a Client object, that receives the query data, and save all the clients in a ArrayList of clients. Once this is done, you pass it to the Request, and you access it from archivo.jsp , and in the repetitive search for each Client object, and from there you get the necessary data to show

    
answered by 03.04.2017 в 13:16
2

The queryForList function returns an Object of type List with a collection of Maps that correspond to each of the rows in the table.

When I had to iterate through Maps lists the notation you indicated does not work, try the following:

<c:forEach items="${datos}" var="dato" varStatus="status">
      <li>${dato["ID"]}</li>
</c:forEach>
    
answered by 05.04.2017 в 16:40
1

You could try putting together a List with the "data" object loaded by hand, to divide the problem.

On the other hand, I recommend that you use the lowercase "dato.id" format in the view, since generally the frameworks when trying to resolve the id property will go to find the setter and getter of that property, that is, that in the class you should have setId () and getId ().

    
answered by 04.04.2017 в 12:17
1

You can see this tutorial where a similar application is explained, the error can be in the controller, the Servlet configuration, the HandlerMapping or the ViewResolver, but without the complete application it is not easy to know, so I recommend the following tutorial: link

    
answered by 08.04.2017 в 17:19