Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.27
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Oct 7 2019 09:57:22 UTC
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.27.0
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Linux
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            5.0.0-32-generic
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /usr/lib/jvm/java-8-openjdk-amd64/jre
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Private Build
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /home/sudarshan/Documents/workspace-spring-tool-suite-4-
4.4.1.RELEASE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /home/sudarshan/Downloads/apache-tomcat-9.0.27
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/home/sudarshan/Documents/workspace-spring-
tool-suite-4-4.4.1.RELEASE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/home/sudarshan/Downloads/apache-tomcat-9.0.27
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/home/sudarshan/Documents/workspace-spring-tool-
suite-4-4.4.1.RELEASE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/home/sudarshan/Downloads/apache-tomcat-
9.0.27/endorsed
Nov 01, 2019 9:51:04 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Nov 01, 2019 9:51:04 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in
production environments was not found on the java.library.path:
[/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-
gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
Nov 01, 2019 9:51:05 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-2016"]
Nov 01, 2019 9:51:05 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Nov 01, 2019 9:51:05 AM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [607] milliseconds
Nov 01, 2019 9:51:05 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Nov 01, 2019 9:51:05 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.27]
Nov 01, 2019 9:51:06 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Nov 01, 2019 9:51:06 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for
this logger for a complete list of JARs that were scanned but no TLDs were found in them.
Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Nov 01, 2019 9:51:06 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'ds'
Nov 01, 2019 9:51:06 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'ds'
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
`com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual
loading of the driver class is generally unnecessary.
Nov 01, 2019 9:51:07 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 746 ms
Nov 01, 2019 9:51:08 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Nov 01, 2019 9:51:08 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for
this logger for a complete list of JARs that were scanned but no TLDs were found in them.
Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Nov 01, 2019 9:51:08 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'ds'
Nov 01, 2019 9:51:08 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'ds'
Nov 01, 2019 9:51:08 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 612 ms
Nov 01, 2019 9:51:08 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-2016"]
Nov 01, 2019 9:51:08 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Nov 01, 2019 9:51:08 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in [3,765] milliseconds
Nov 01, 2019 9:51:09 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /StudentCurd/


package StudentCurd;

public class Student
{
int id;
String name;
String email;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

}


package StudentCurd;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;

@Controller
public class StudentController
{
@Autowired
StudentDao dao;

@RequestMapping("/studentform")
public String showForm(Model m)
{
     m.addAttribute("command", new Student());
     return "studentform";

}

@RequestMapping(value="/save",method = RequestMethod.POST)
public String save(@ModelAttribute("s") Student s){
    dao.save(s);
    return "redirect:/viewemp"; //will redirect to viewemp request mapping
}

@RequestMapping("/viewemp")
public String viewemp(Model m){
    List<Student> list=dao.getStudents();
    m.addAttribute("list",list);
    return "viewemp";
}

@RequestMapping(value="/editemp/{id}")
public String edit(@PathVariable int id, Model m){
    Student s=dao.getStudentById(id);
    m.addAttribute("command",s);
    return "empeditform";
}

@RequestMapping(value="/editsave",method = RequestMethod.POST)
public String editsave(@ModelAttribute("s") Student s){
    dao.update(s);
    return "redirect:/viewemp";
}

@RequestMapping(value="/deleteemp/{id}",method = RequestMethod.GET)
public String delete(@PathVariable int id){
    dao.delete(id);
    return "redirect:/viewemp";
}

}


package StudentCurd;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import StudentCurd.Student;

public class StudentDao
{

JdbcTemplate template;

public void setTemplate(JdbcTemplate template) {
    this.template = template;
}

public int save(Student s){
    String sql="insert into student(id,name,email)
values('"+s.getId()+"',"+s.getName()+",'"+s.getEmail()+"')";
    return template.update(sql);
}

public int update(Student s){
    String sql="update student set id='"+s.getId()+"',
name="+s.getName()+",email='"+s.getEmail()+"' where id="+s.getId()+"";
    return template.update(sql);
}

public int delete(int id){
    String sql="delete from student where id="+id+"";
    return template.update(sql);
}

public Student getStudentById(int id){
    String sql="select * from student where id=?";
    return template.queryForObject(sql, new Object[]{id},new BeanPropertyRowMapper<Student>
(Student.class));
}

public List<Student> getStudents(){
    return template.query("select * from student",new RowMapper<Student>(){
        public Student mapRow(ResultSet rs, int row) throws SQLException {
            Student s=new Student();
            s.setId(rs.getInt(1));
            s.setName(rs.getString(2));
            s.setEmail(rs.getString(3));

            return s;
        }
    });
}

}


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
`xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-
app_3_0.xsd" id="WebApp_ID" version="3.0">`
  <display-name>StudentMvc</display-name>
  <servlet>
    <servlet-name>ds</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ds</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>


<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

 <context:component-scan base-package="StudentCurd" />

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>



  <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/userdb"></property>
  <property name="username" value="root"></property>
  <property name="password" value="password"></property>
  </bean>

  <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="ds"></property>
  </bean>

  <bean id="dao" class="StudentCurd.StudentDao">
  <property name="template" ref="jt"></property>
  </bean>

  </beans>


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <a href="studentform">Add Student</a>
 <a href="viewstudent">View Students</a>
</body>
</html>


<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



    <h1>Edit Employee</h1>
   <form:form method="POST" action="/StudentCurd/editsave">
    <table >
    <tr>
    <td></td>
     <td><form:hidden path="idh" /></td>
     </tr>
     <tr>
      <td>Id : </td>
      <td><form:input path="id"  /></td>
     </tr>
     <tr>
      <td>Name :</td>
      <td><form:input path="name" /></td>
     </tr>
     <tr>
      <td>Email :</td>
      <td><form:input path="email" /></td>
     </tr>

     <tr>
      <td> </td>
      <td><input type="submit" value="Edit Save" /></td>
     </tr>
    </table>
   </form:form>


<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



    <h1>Add New Student</h1>
    <form:form method="post" action="save">
    <table >
     <tr>
      <td>Id : </td>
      <td><form:input path="id"  /></td>
     </tr>
     <tr>
      <td>Name :</td>
      <td><form:input path="name" /></td>
     </tr>
     <tr>
      <td>Email :</td>
      <td><form:input path="email" /></td>
     </tr>
     <tr>
      <td> </td>
      <td><input type="submit" value="Save" /></td>
     </tr>
    </table>
   </form:form>


<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<h1>Student List</h1>
<table border="2" width="70%" cellpadding="2">
<tr><th>Id</th><th>Name</th><th>Email</th><th>Edit</th><th>Delete</th></tr>
<c:forEach var="s" items="${list}">
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.email}</td>
<td><a href="editemp/${emp.id}">Edit</a></td>
<td><a href="deleteemp/${emp.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
<br/>
<a href="studentform">Add New Student</a>

最佳答案

您的错误告诉您问题出在哪里。您的 Controller 中没有与“/ StudentCurd /”的映射。

更有可能的问题是,您认为上下文路径为/ StudentCurd(程序包的名称),而不是。与其尝试访问“/ StudentCurd / editsave”,不如访问“/ editsave”。

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



    <h1>Edit Employee</h1>
   <form:form method="POST" action="/StudentCurd/editsave">    <-- remove /StudentCurd
    <table >
    <tr>
    <td></td>
     <td><form:hidden path="idh" /></td>
     </tr>
     <tr>
      <td>Id : </td>
      <td><form:input path="id"  /></td>
     </tr>
     <tr>
      <td>Name :</td>
      <td><form:input path="name" /></td>
     </tr>
     <tr>
      <td>Email :</td>
      <td><form:input path="email" /></td>
     </tr>

     <tr>
      <td> </td>
      <td><input type="submit" value="Edit Save" /></td>
     </tr>
    </table>
   </form:form>

编辑:

如果您的上下文路径位于/ StudentCurd(您将Webapp部署在/ StudentCurd而不是ROOT位置),则将收到此错误,因为您没有“/”的映射。您应该具有一个基本索引模板(index.jsp),该模板充当您的主页,并映射到“/”。如果您尝试访问“/ StudentCurd / studentform”,那么它应该可以工作,只要您的上下文路径是/ StudentCurd而不是ROOT(在这种情况下,您将只访问“/ studentform”)

在您的 Controller 中:
@RequestMapping("/")
public String home() {
   return "index";
}

这样的事情。如果您希望学生表格成为主页,则只需更改映射即可:
@RequestMapping("/")
public String showForm(Model m)
{
     m.addAttribute("command", new Student());
     return "studentform";

}

07-24 21:35