我正在尝试在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'.
答案 0 :(得分:1)
将您的xml更改为set 8dp elevation
,如下所示。
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
答案 1 :(得分:1)
我会选择无版本的定义(参见下面的xml) Spring正在根据每个spring jar中的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