我是SpringFramework的新手,我正在尝试制作一个Spring MVC Web应用程序(使用数据库)。我从标题中得到了这个错误,我不明白为什么。我检查了文件,每一件事似乎都很好。我正在添加文件:
EmployeeController.java
package com.javatpoint.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.javatpoint.beans.Employee;
import com.javatpoint.dao.EmployeeJDBCTemplate;
@Controller
public class EmployeeController {
@Autowired
EmployeeJDBCTemplate jdbcTemplate;//will inject dao from xml file
/*It displays a form to input data, here "command" is a reserved request attribute
*which is used to display object data into form
*/
@RequestMapping("/empform")
public ModelAndView showform(){
return new ModelAndView("empform","command",new Employee());
}
/*It saves object into database. The @ModelAttribute puts request data
* into model object. You need to mention RequestMethod.POST method
* because default request is GET*/
@RequestMapping(value="/save", method=RequestMethod.POST)
public ModelAndView save(@ModelAttribute("emp") Employee employee){
jdbcTemplate.save(employee);
return new ModelAndView("redirect:/viewmap"); //will redirect to viewmap request mappeing
}
/* It provides list of employees in model object */
@RequestMapping("/viewmap")
public ModelAndView viewmap(){
List<Employee> list = jdbcTemplate.getEmployees();
return new ModelAndView("viewmap","list",list);
}
/* It displays object data into form for the given id.
* The @PathVariable puts URL data into variable.*/
@RequestMapping(value="/editemp/{id}")
public ModelAndView edit(@PathVariable int id){
Employee employee = jdbcTemplate.getEmployee(id);
return new ModelAndView("empeditform","command",employee);
}
/* It updates model object. */
@RequestMapping(value="/editsave",method=RequestMethod.POST)
public ModelAndView editsave(@ModelAttribute("emp") Employee employee){
jdbcTemplate.update(employee);
return new ModelAndView("redirect:/viewemp");
}
/* It deletes record for the given id in URL and redirects to /viewemp */
@RequestMapping(value="/deleteemp/{id}",method = RequestMethod.GET)
public ModelAndView delete(@PathVariable int id){
jdbcTemplate.delete(id);
return new ModelAndView("redirect:/viewemp");
}
}
EmployeeJDBCTemplate.java
package com.javatpoint.dao;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.javatpoint.beans.Employee;
public class EmployeeJDBCTemplate implements EmployeeDao{
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;
@Override
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
@Override
public void save(Employee employee) {
String SQL = "insert into emp99 (id,name,salary,designation) values (?,?,?,?)";
jdbcTemplateObject.update(SQL,employee.getId(),employee.getName(),employee.getSalary(),employee.getDesignation());
}
@Override
public void update(Employee employee) {
String SQL = "update emp99 set name=?, salary=?, designation=? where id=?";
jdbcTemplateObject.update(SQL,employee.getName(),employee.getSalary(),employee.getDesignation(),employee.getId());
}
@Override
public void delete(int id) {
String SQL = "delete from emp99 where id=?";
jdbcTemplateObject.update(SQL,id);
}
@Override
public Employee getEmployee(int id) {
String SQL = "select * from emp99 where id=?";
Employee employee = jdbcTemplateObject.queryForObject(SQL, new Object[]{id}, new EmployeeMapper());
return employee;
}
@Override
public List<Employee> getEmployees() {
String SQL = "select * from emp99";
List<Employee> employees = jdbcTemplateObject.query(SQL, new EmployeeMapper());
return employees;
}
}
调度-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.javatpoint.controllers"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/jdbc_test"/>
<property name="username" value="aaaa"/>
<property name="password" value="1234"/>
</bean>
<bean id="jdbcTemplate" class="com.javatpoint.dao.EmployeeJDBCTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:-1)
您的dispatcher-servlet.xml中缺少<mvc:annotation-driven />
,告诉spring使用注释。
名称空间为xmlns:mvc="http://www.springframework.org/schema/mvc"
,schemaLocation为http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd