2016-01-20 60 views
0

我想在spring mvc中創建一個程序,但出現錯誤。這裏是程序Spring MVC匹配通配符嚴格錯誤

的index.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> 
    <jsp:directive.page contentType="text/html; charset=ISO-8859-1" 
     pageEncoding="ISO-8859-1" session="false"/> 
    <jsp:output doctype-root-element="html" 
     doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
     doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
     omit-xml-declaration="true" /> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Welcome</title> 
</head> 
<body> 
<a href="welcome">Welcome Guest</a> 
</body> 
</html> 
</jsp:root> 

HelloController.java

package java4s; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.ui.ModelMap; 
import org.springframework.format.annotation.DateTimeFormat; 
import java.util.ArrayList; 
import java.util.List; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import org.springframework.beans.factory.annotation.Autowired; 

import java4s.EmployeeService; 
@Controller 
public class HelloController { 

    @Autowired 
    EmployeeService emp_service; 

    @RequestMapping("/welcome") 
     public ModelAndView helloWorld(@ModelAttribute("userForm") Employee employee, ModelMap model) { 

      String message = "Welcome to Java4s.com Spring MVC 4.1.1 Sessions"; 
        message += "<br>You Did it....!"; 
        List<String> professionList = new ArrayList(); 
        professionList.add("Developer"); 
        professionList.add("Designer"); 
        professionList.add("IT Manager"); 
        model.put("professionList", professionList); 
      return new ModelAndView("welcomePage", "welcomeMessage", new Employee()); 
     } 
     @RequestMapping(value = "/addemployee", method=RequestMethod.POST) 
     @DateTimeFormat(pattern="MM/dd/yyyy") 
    public ModelAndView submitForm(ModelMap model, @ModelAttribute("userForm") Employee employee/*, BindingResult errors*/) { 

      /*if(errors.hasErrors()) 
      { 
       model.addAttribute("studenterrors", "Errorsssss"); 
      }*/ 
      //System.out.println(student.getBirthdate()); 
      //model.addAttribute("studenterrors", "Errorsssss"); 
      model.addAttribute("username", employee.getUsername()); 
      model.addAttribute("password", employee.getPassword()); 
      model.addAttribute("birthdate", employee.getBirthdate()); 
      model.addAttribute("email", employee.getEmail()); 
      model.addAttribute("professionList", employee.getProfession()); 
      model.addAttribute("userForm", new Employee()); 

      emp_service.saveData(employee); 
      return new ModelAndView("RegisterSuccess",model); 
    } 

} 

EmployeeServiceImpl.java

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package java4s; 

import java4s.Employee; 
import org.springframework.jdbc.core.JdbcTemplate; 
import javax.sql.DataSource; 
import org.springframework.beans.factory.annotation.Autowired; 

/** 
* 
* @author Harshit Shrivastava 
*/ 
public class EmployeeServiceImpl implements EmployeeService { 

    @Autowired 
    DataSource dataSource; 

    @Override 
    public void saveData(Employee employee) 
    { 

     String query = "INSERT INTO EmployeeInfo(userid,username,firstname,lastname,mobileno,emailid,password,profession) VALUES(?,?,?,?,?,?,?,?)"; 

     JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 


    } 
} 

歡迎-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"> 

    <context:component-scan base-package="java4s" /> 
    <mvc:annotation-driven /> 
    <bean id="EmployeeService" class="java4s.EmployeeServiceImpl" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
    <bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="oracle.jdbc.pool.OracleDataSource" 
    p:url="jdbc:oracle:thin:@localhost:1521:IM" 
    p:username="user" 
    p:password="pass" /> 
</beans> 

無論何時提交表單,我都會收到此錯誤。 錯誤:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 14 in XML document from ServletContext resource [/WEB-INF/welcome-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 27; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'. 

回答

1

更改XML有http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd如下。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:component-scan base-package="java4s" /> 
    <mvc:annotation-driven /> 
    <bean id="EmployeeService" class="java4s.EmployeeServiceImpl" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
    <bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="oracle.jdbc.pool.OracleDataSource" 
    p:url="jdbc:oracle:thin:@localhost:1521:IM" 
    p:username="user" 
    p:password="pass" /> 
</beans> 
+0

同樣的錯誤,我越來越! –

1

我會去爲無版本定義(請參閱下面的XML)。
Spring在每個彈簧罐中都基於META-INF/spring.schemas來確定要使用的模式。
確保您不混合使用彈簧版本,並且在項目中沒有自己的META-INF/spring.schemas。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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" 
    > 

    <context:component-scan base-package="java4s" /> 
    <mvc:annotation-driven /> 
    <bean id="EmployeeService" class="java4s.EmployeeServiceImpl" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
    <bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="oracle.jdbc.pool.OracleDataSource" 
    p:url="jdbc:oracle:thin:@localhost:1521:IM" 
    p:username="user" 
    p:password="pass" /> 
</beans> 

又見Spring configuration XML schema: with or without version? http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/extensible-xml.html

相關問題