2013-07-13 114 views
1

我需要一个与春季休眠和MySQL的项目,主页工作正常(它甚至从MySQL中获取数据并显示它),但是当我点击添加/编辑或删除按钮我获取404错误的描述客户端发送的请求在语法上不正确。 我的学生Controller.java春季Hibernate与maven和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"; 
}} 

我的web.xml文件

<?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> 

我的春天servlet.xml文件

<?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> 

我spring1-servlet.xml文件是

<?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> 

我student.jsp是

<%@ 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> 

与这个家伙的任何帮助吗?!我的意思是它能够连接到数据库perectly罚款我不知道为什么按钮不起作用

+0

你可以粘贴视图,你有编辑按钮? – zerocool

+0

Zerocool对不起,我忘了发布我的student.jsp文件在这里它是 –

回答

0

我看到以下两个问题:

第一期:

在你的控制器,可以使用

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

但在你的形式使用

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

您的表单是否也使用/student.do?唯一的原因我怀疑这是因为你已经表明你尝试添加/更新时出现404(页面未找到)错误。

第二期:

您使用4提交每个按钮都有自己的名字

<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" /> 

但是,你的控制器方法的@RequestParam假定你将在一个PARAM获得的价值。

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

首先,你应该改变它有一个名字:

 @RequestParam("action") String action, 

,其次添加一个隐藏字段(<input type="hidden" name="action" />)开展行动,服务器的价值,并确保您更新它的值通过将点击处理程序附加到四个操作按钮上,使用Java脚本“添加”,“编辑”,“删除”或“搜索”。值得关注的

第三个可能的原因:

试图摆脱第二个调度的servlet,并且只使用一个,如果可能的。因此,使默认的一个手柄*.dosome people have faced issues when using two dispatcher servlet。我确信它可以工作,但很难使用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> 

,并把所有的@RequestMapping使用.do,如:index.dostudent.do

这个答案是基于什么我可以从这个问题做出来。

+0

我重命名所有的按钮行动本身,我改变了显示的student.do到/student.do,仍然得到HTTP 404错误找不到文件/学生.do? –

+0

什么是可用的网址?用于列表页面的页面可以工作,您可以分享该URL以及不起作用的URL。我的意思是以“http:// ...”开头的完整URL。 –

+0

添加注释以仅使用一个调度程序servlet。发现这个链接人们面临类似的问题:http://forum.springsource.org/showthread.php?10366-Multiple-dispatcher-servlets但没有明确的答案 –