私はSpringFrameworkで新しく、データベースを使用してSpring MVC Webアプリケーションを作成しようとしています。私はタイトルからそのエラーを受けているし、なぜ私は理解していない。私はファイルをチェックし、すべてがうまくいくようだ。私は、ファイルを追加している:名前が 'dispatcher'のDispatcherServletのURI [/ MVCCrud/viewemp]を持つHTTP要求のマッピングが見つかりません
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>
URLはviewmapで、質問にあなたがviewmp言っています? –
@nikeshjoshiなぜviewempをviewmapに変更したのか分かりません。ありがとう。それは今働く。 –
本当に??それは働いています –