简体   繁体   中英

No mapping found for HTTP request with URI [/MVCCrud/viewemp] in DispatcherServlet with name 'dispatcher'

I'm new at SpringFramework and I'm trying to make a Spring MVC web app (using database). I am getting that error from the title and I don't understand why. I checked the files and everyting seems to be fine. I'm adding the files:

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;
    }
}

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

I am adding a photo with the files structure too. 在此处输入图片说明

Your are missing the <mvc:annotation-driven /> in your dispatcher-servlet.xml to tell spring to use annotations.

The namespace is xmlns:mvc="http://www.springframework.org/schema/mvc" and the schemaLocation http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM