2013-07-13 2 views
1

Je dois avoir un projet avec spring hibernate et mysql, la page d'accueil fonctionne bien (elle extrait même les données de mysql et l'affiche) mais quand je clique sur le bouton add/edit ou delete i obtenir une erreur 404 avec la description La requête envoyée par le client était syntaxiquement incorrecte. Mon étudiant Controller.javaIntégration Spring Hibernate avec maven et mysql

package com.joseph.controller; 

import java.util.Map; 

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.bind.annotation.RequestParam; 

import com.joseph.model.Student; 
import com.joseph.service.StudentService; 

@Controller 
public class StudentController { 
@Autowired 
private StudentService studentService; 

@RequestMapping("/index") 
public String setupForm(Map<String, Object> map){ 
    Student student = new Student(); 
    map.put("student", student); 
    map.put("studentList", studentService.getAllStudent()); 
    return "student"; 
} 

@RequestMapping(value="/student.do", method=RequestMethod.POST) 
public String doActions(@ModelAttribute Student student, BindingResult result, @RequestParam String action, Map<String, Object> map){ 
//  System.out.println("inside doAction"); 
    Student studentResult = new Student(); 
// System.out.println("after student object"); 
    switch(action.toLowerCase()){//only in Java7 you can put String in switch 
    case "add": 
     studentService.add(student); 
     studentResult = student; 
     System.out.println("Inside case action value is - add"); 
     break; 
    case "edit": 
     studentService.edit(student); 
     studentResult = student; 
     System.out.println("Inside case action value is - edit"); 
     break; 
    case "delete": 
     studentService.delete(student.getStudentId()); 
     studentResult = new Student(); 
     System.out.println("Inside case action value is - delete"); 
     break; 
    case "search": 
     Student searchedStudent = studentService.getStudent(student.getStudentId()); 
     studentResult = searchedStudent!=null ? searchedStudent : new Student(); 
     System.out.println("Inside case action value is - search"); 
     break; 
    } 
    System.out.println("after switch"); 
    map.put("student", studentResult); 
    map.put("studentList", studentService.getAllStudent()); 
    return "student"; 
}} 

Mon fichier web.xml est

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

<context-param> 
    <param-name>log4jConfigLocation</param-name> 
    <param-value>classpath:log4j.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
</servlet-mapping> 
<servlet> 
<servlet-name>spring1</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>spring1</servlet-name> 
<url-pattern>*.do</url-pattern> 
</servlet-mapping> 
    <welcome-file-list> 
<welcome-file>index.html</welcome-file> 
<welcome-file>index.htm</welcome-file> 
<welcome-file>index.jsp</welcome-file> 
<welcome-file>default.html</welcome-file> 
<welcome-file>default.htm</welcome-file> 
<welcome-file>default.jsp</welcome-file> 

Mon fichier servlet.xml printemps

<?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:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" 
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 



<context:annotation-config /> 
<context:component-scan base-package="com.joseph" />  


<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:location="/WEB-INF/jdbc.properties" /> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> 
<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation"> 
     <value>classpath:hibernate.cfg.xml</value> 
    </property> 
    <property name="configurationClass"> 
     <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 
<tx:annotation-driven /> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

Mon fichier printemps1-servlet.xml est

<?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:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" 
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 



<context:annotation-config /> 
<context:component-scan base-package="com.joseph" />  


<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:location="/WEB-INF/jdbc.properties" /> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> 
<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation"> 
     <value>classpath:hibernate.cfg.xml</value> 
    </property> 
    <property name="configurationClass"> 
     <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
    <property name="prefix" value="/WEB-INF/jsp/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 
<tx:annotation-driven /> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

Mon student.jsp est

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ include file="/WEB-INF/jsp/includes.jsp"%> 
<!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>Student Management</title> 
    </head> 
    <body> 
      <h1>Students Data</h1> 
<form:form action="student.do" method="POST" commandName="student"> 

<table> 
    <tr> 
     <td>Student ID</td> 
     <td><form:input path="studentId" /></td> 
    </tr> 
    <tr> 
     <td>First name</td> 
     <td><form:input path="firstname" /></td> 
    </tr> 
    <tr> 
     <td>Last name</td> 
     <td><form:input path="lastname" /></td> 
    </tr> 
    <tr> 
     <td>Year Level</td> 
     <td><form:input path="yearLevel" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
      <input type="submit" name="action1" value="Add" /> 
      <input type="submit" name="action2" value="Edit" /> 
      <input type="submit" name="action3" value="Delete" /> 
      <input type="submit" name="action4" value="Search" /> 
     </td> 
    </tr> 
</table> 
     </form:form> 
     <br> 
      <table border="1"> 
     <th>ID</th> 
     <th>First name</th> 
     <th>Last name</th> 
     <th>Year level</th> 
      <c:forEach items="${studentList}" var="student"> 
        <tr> 
        <td>${student.studentId}</td> 
        <td>${student.firstname}</td> 
       <td>${student.lastname}</td> 
       <td>${student.yearLevel}</td> 
    </tr> 
     </c:forEach > 
</table> 
    </body> 
</html> 

Toute aide avec ce gars?! Je veux dire son pouvoir de se connecter à la base de données que j'ai perectly bien aucune idée pourquoi les boutons ne fonctionnent pas

+0

Pouvez-vous coller la vue, dans laquelle vous avez le bouton d'édition? – zerocool

+0

Zerocool désolé j'ai oublié de poster mon fichier student.jsp ici c'est –

Répondre

0

Je vois suite à deux problèmes:

Première question:

Dans votre contrôleur, vous utilisez

@RequestMapping(value="/student.do", method=RequestMethod.POST) 

mais dans votre formulaire, vous utilisez

<form:form action="student.do" method="POST" commandName="student"> 

Votre formulaire ne doit pas utiliser également /student.do? La seule raison que je soupçonne c'est parce que vous avez indiqué que vous obtenez une erreur 404 (Page introuvable) en essayant d'ajouter/de mettre à jour.

Deuxième question:

Vous utilisez quatre boutons d'envoi chacun avec son propre nom

<input type="submit" name="action1" value="Add" /> 
<input type="submit" name="action2" value="Edit" /> 
<input type="submit" name="action3" value="Delete" /> 
<input type="submit" name="action4" value="Search" /> 

mais la @RequestParam de votre méthode de commande suppose que vous obtiendrez la valeur dans un param.

public String doActions(@ModelAttribute Student student, 
     BindingResult result, 
     @RequestParam String action, //***THIS IS WRONG**** 
     Map<String, Object> map){ 

D'abord, vous devez changer de nom:

 @RequestParam("action") String action, 

et, deuxièmement ajouter un champ caché (<input type="hidden" name="action" />) pour porter la valeur de l'action au serveur et assurez-vous que vous mettez à jour sa valeur "Ajouter", "Modifier", "Supprimer" ou "Rechercher" en utilisant Java Script en attachant des gestionnaires on-click à quatre boutons d'action.

Troisième Cause possible préoccupante:

Essayez de se débarrasser de deuxième servlet répartiteur, et utiliser une seule si possible. Donc, faites en sorte que la valeur par défaut soit *.do en tant que some people have faced issues when using two dispatcher servlet. Je suis sûr qu'il peut être fait fonctionner mais il est difficile de localiser l'erreur à distance en utilisant SO.

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>*.do</url-pattern> 
</servlet-mapping> 

et ont tous vos @RequestMapping utilisation .do, par exemple: index.do et student.do

Cette réponse est basée sur ce que je peux faire à partir de la question.

+0

J'ai renommé tous les boutons à l'action elle-même et j'ai changé student.do dans l'affichage de /student.do, obtenant toujours l'erreur Http 404 ne trouve pas le fichier/étudiant .faire ? –

+0

Quelle est l'URL qui fonctionne? Celui de la page de liste fonctionne donc, pouvez-vous partager cette URL avec l'URL qui ne fonctionne pas. Je veux dire l'URL complète commençant par "http: // ...." –

+0

Ajout d'un commentaire pour utiliser une seule servlet du répartiteur. Trouvé ce lien où les gens font face à un problème similaire: http://forum.springsource.org/showthread.php?10366-Multiple-dispatcher-servlets mais pas de réponse claire –

Questions connexes