提交时JSP + Spring + MySQL Populate表

时间:2013-10-17 11:19:46

标签: mysql jsp spring-mvc

我是JSP的新手,我是一名前端开发人员,但出于工作原因,我正在努力实现服务器端。

目前我正在使用JSP + Spring + MySQL开发一个“测试”网站。 昨天,我能够在数据库的数据空间中登录和注销验证用户,而不是没有苦苦挣扎。

我今天要实现的目标是在表单的提交事件中填充表格。

得到了这个, USERPANEL.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Success</title>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
      <!--script src="./js/ajax.js"></script-->
   </head>
   <body>
      <h1>Hello ${applicationModel.loginModel.userName}!</h1>
      <div class="user-control-panel">
         <form:form id="logout" action="logout.do" method="post" commandName="logoutDetails">
            <input type="submit" value="Logout" />
            <input type="hidden" name="userName" id="userName" value="${applicationModel.loginModel.userName}" />
            <input type="hidden" name="debugmode" id="debugmode" value="${applicationModel.loginModel.debugmode}" />         
         </form:form>
      </div>
      <div>
         <form:form id="listAll" action="movies.do" method="post" commandName="moviesDetails">
            <input type="submit" value="List movies" />
            <input type="hidden" name="minLim" value="-1" />
            <input type="hidden" name="maxLim" value="-1" />
            <table>
               <thead>
                  <tr>
                     <th>ID</th>
                     <th>Title</th>
                     <th>Year</th>
                     <th>Genre</th>
                     <th>Subtitled</th>
                     <th>Dual</th>
                     <th>Poster</th>
                     <th>Link</th>
                  </tr>
               </thead>
               <tbody>
                  <c:forEach var="movie" items="${applicationModel.moviesModel.list}">
                     <tr>
                        <td>${movie.movieID}</td>
                        <td>${movie.titulo}</td>
                        <td>${movie.anyo}</td>
                        <td>${movie.genero}</td>
                        <td>${movie.subtitulada}</td>
                        <td>${movie.dual}</td>
                        <td>${movie.poster}</td>
                        <td>${movie.megaLink}</td>
                     </tr>
                  </c:forEach>
               </tbody>
            </table>
         </form:form>
      </div>      
   </body>
</html>

MOVIESCONTROLLER.java

package com.wolfchamane.controller;

import com.wolfchamane.model.MoviesModel;
import com.wolfchamane.service.MoviesService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
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 org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping(value = "/movies")
public class MoviesController {

   private MoviesModel moviesModel;

   @Autowired
   private MoviesService moviesService;

   @RequestMapping(method = RequestMethod.GET)
   public ModelAndView init(){
      moviesModel = new MoviesModel();
      return new ModelAndView("movies", "moviesDetails", moviesModel);
   }

   @RequestMapping(method = RequestMethod.POST)
   public ModelAndView listAll(
           @ModelAttribute("moviesDetails") MoviesModel moviesModel,
           BindingResult bindingResult,
           HttpServletRequest request,
           HttpServletResponse response){

      ModelAndView redirectView = null;


      if (moviesService.listAll(moviesModel)){         
         RedirectView view = new RedirectView("userpanel.do", true);         
         redirectView = new ModelAndView(view);
      }

      return redirectView;
   }  
}

MOVIESDAOIMP.java

package com.wolfchamane.dao;

import com.wolfchamane.model.MovieModel;
import java.sql.*;
import com.wolfchamane.model.MoviesModel;
import java.util.ArrayList;

public class MoviesDAOImp implements MoviesDAO{

   @Override
   public boolean listAll(MoviesModel moviesModel) {
      boolean bRet = false;
      PreparedStatement pst = null;
      Statement st = null;

      try{

         String query;
         ResultSet rs = null;

         //SELECT ALL
         if ((moviesModel.getMinLim() < 0)&&(moviesModel.getMaxLim() < 0)){
            query = "SELECT" 
               +" m.movie_id AS \"ID\", m.movie_title AS \"TITULO\","
               +" m.movie_year AS \"ANYO\", g.genero_text_esES AS \"GENERO\","
               +" m.subtitulada AS \"SUBS\", m.`dual` AS \"DUAL\","
               +" m.poster AS \"POSTER\", m.megaLink AS \"LINK\""
               +" FROM mymovies_movies m, mymovies_generos g"
               +" WHERE (m.genero = g.genero_id)"
               +" ORDER BY m.movie_title;";
            st = ConnectionDAO.getStatement(moviesModel.isDebugmode());
            rs = st.executeQuery(query);
         }

         if (rs != null){
            ArrayList<MovieModel> list = new ArrayList();
            while(rs.next()){
               MovieModel aux = new MovieModel();
               aux.setMovieID(rs.getInt("ID"));
               aux.setTitulo(rs.getString("TITULO"));
               aux.setAnyo(rs.getInt("ANYO"));
               aux.setGenero(rs.getString("GENERO"));
               aux.setSubtitulada((rs.getInt("SUBS") == 1));
               aux.setDual((rs.getInt("DUAL") == 1));
               aux.setPoster(rs.getString("POSTER"));
               aux.setMegaLink(rs.getString("LINK"));
               list.add(aux);
            }//while
            moviesModel.setList(list);
         }
         else
            bRet = false;         

         if (st != null)
            st.close();

         if (pst != null)
            pst.close();

         if (rs != null)
            rs.close();
      }catch(SQLException sql){
         System.out.println("SQL Exception: "+sql.getMessage());
         bRet = false;
      }finally{
         return bRet;
      }
   }

}

MOVIESSERVICEIMP.java

package com.wolfchamane.service;

import com.wolfchamane.dao.MoviesDAO;
import com.wolfchamane.model.MoviesModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("MoviesService")
public class MoviesServiceImp implements MoviesService{

   @Autowired
   MoviesDAO moviesDAO;

   @Override
   public boolean listAll(MoviesModel moviesModel){
      return moviesDAO.listAll(moviesModel);
   }

}

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

   <display-name>MyMoviesAPP</display-name> 

    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login.jsp</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>dashboard</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dashboard</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

SERVLETS

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.wolfchamane" />    

    <mvc:annotation-driven />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.wolfchamane" />    

    <mvc:annotation-driven />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>    

</beans>

我总是收到以下错误:

javax.servlet.ServletException: Servlet.init() para servlet login lanzó excepción
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:722)

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'moviesController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wolfchamane.service.MoviesService com.wolfchamane.controller.MoviesController.moviesService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MoviesService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.wolfchamane.dao.MoviesDAO com.wolfchamane.service.MoviesServiceImp.moviesDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.wolfchamane.dao.MoviesDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:722)

我应该更改/添加/修改什么? 前一个想法是,当记录用户点击“全部列出”按钮时,下表将填充数据库中的数据。

1 个答案:

答案 0 :(得分:0)

您是否为扫描包指定了<context:component-scan base-package="com.telemedicine." /> 其中包含“MovieService”。使用@Repository in dao impl

建议:使用SimpleJdbcTemplate + SimpleJdbcDaoSupport,它将删除JDBC样板代码。