Spring Mvc学生注册表

时间:2017-08-10 06:07:59

标签: java spring spring-mvc tomcat

我是春天的新手。我试图编写以下程序,但它给出了404错误。我无法跟踪确切错误的位置

throwing HTTP Status 404 - /submitAdmissionForm.html.
HTTP Status 404 - /submitAdmissionForm.html

输入状态报告

message /submitAdmissionForm.html

说明请求的资源不可用。

enter image description here

Apache Tomcat / 8.0.28

我的档案:

欢迎页面 - index.jsp

<html>
<body>
    <h3> STUDENT ADMISSION FORM FOR ENGINEERING COURSES</h3>

    <form action="/submitAdmissionForm.html" method="post">
        Student's Name : <input type="text" name="studentName" />
        Student's Hobby :<input type="text" name="studentHobby" />
        Student's Mobile :<input type="text" name="studentMobile" />
        Student's DOB :<input type="text" name="studentDOB" />
        Student's Skills set
        <select name="studentSkills" >
        <option value="Java Core">Java Core</option>
        <option value="Spring Core">Spring Core</option>
        <option value="Spring MVC">Spring MVC</option>
        </select>
        Student's Address :  

            country: <input type="text" name="studentAddress.country"/>
            city: <input type="text" name="studentAddress.city" />
            street: <input type="text" name="studentAddress.street" /> 
            pincode:<input type="text" name="studentAddress.pincode" />
        <input type="submit" value="Submit this form by clicking here" />
    </form>

</body>
</html>

描述符 的web.xml

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>praygod</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
<servlet>
<servlet-name>student</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>student</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

学生servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


  <context:component-scan base-package="com.gappu.student" />
  <mvc:annotation-driven/>

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


</beans>

Controller.java

package com.gappu.student;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class studentcontroller {



    @RequestMapping(value="/submitAdmissionForm.html", method = RequestMethod.POST)
    public ModelAndView submitAdmissionForm() {



        ModelAndView model1 = new ModelAndView();
        model1.setViewName("AdmissionSuccess");
        return model1;
    }



}

这是我的成功页面

AdmissionSuccess.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    Hello there
    </body>
    </html> 

当我尝试运行此程序时,由于404建议URL不存在而导致错误。

2 个答案:

答案 0 :(得分:1)

我认为问题在于您的项目不包含所有库。所以,如果您使用Eclipse和Maven,请转到

项目属性 - &gt;部署组件 - &gt;添加 - &gt; Java构建路径条目 - &gt; Maven Dependencies。

答案 1 :(得分:0)

 web.xml
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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>HelloWorldWebApp</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
messages.properties
NotEmpty.student.studentName={0} can't be empty
Pattern.student.studentName=Number is not allowed for {0}
Size.student.studentName={0} field is between {2} and {1}
NotEmpty.student.gender=Please select gender {0}
NotNull.student.technology=Select at least one technology
Size.student.technology=Select at least one {0}
NotEmpty.student.city=Please select {0}
NotEmpty.student.email={0} can't be empty
Email.student.email=Please enter valid {0}
NotEmpty.student.password={0} can't be empty
NotNull.student.phone={0} no can't be empty
Min.student.phone=Please enter valid {0}
NotEmpty.studentCredential.email={0} can't be empty
Email.studentCredential.email=Please enter valid {0}
NotEmpty.studentCredential.password={0} can't be empty

dispatcher-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:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.infotech.controller"></context:component-scan>
    <context:component-scan base-package="com.infotech.service.impl"></context:component-scan>
    <context:component-scan base-package="com.infotech.dao.impl"></context:component-scan>
<context:component-scan base-package="com.infotech.util"></context:component-scan>
    <mvc:resources mapping="/image/**" location="/image/" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver.class.name}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>



    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>

        <property name="annotatedClasses">
            <array>
                <value>com.infotech.model.Student</value>
            </array>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>

    </bean>

     <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />

    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <array>
                <value>/WEB-INF/database.properties</value>
            </array>
        </property>
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages"></property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="20848820" />
    </bean>

</beans>

database.properties
driver.class.name=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/da
db.username=root
db.password=root

 view folder
 home.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%>

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Employee Management Screen</title>
</head>
<body>
    <div align="center">
        <h1>Employee List</h1>
        <h3>
            <a href="newEmployee">New Employee</a>
        </h3>
        <table border="1">
 <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Address</th>
            <th>Telephone</th>
            <th>Action</th>
             <th>Action</th>
              <th>Action</th>
               <th>Action</th>
 </tr>
            <c:forEach var="student" items="${listEmployee}">
                <tr>

                    <td>${student.studentName}</td>
                    <td>${student.gender}</td>
                    <td>${student.technology}</td>
                    <td>${student.city}</td>
                    <td>${student.email}</td>
                    <td>${student.password}</td>
                    <td>${student.phone}</td>
                     <td>${student.image}</td>
                    <td><a href="editEmployee?id=${student.id}">Edit</a>

                        <a href="deleteEmployee?id=${student.id}">Delete</a></td>

                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>


register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
<style type="text/css">
.error {
    color: red;
}
</style>
</head>
<body>
    <h3 align="center">${headerMessage}</h3>
    <form:form action="registerSuccess"
        method="post" modelAttribute="student" enctype="multipart/form-data">
        <table align="center">
        <caption align="top">Student registration form</caption>
            <tr>
                <td>Enter Email:</td>
                <td><form:input path="email" /></td>
                <td><form:errors path="email" cssClass="error"/></td>
            </tr>
            <tr>
                <td>Enter Password:</td>
                <td><form:password path="password"/></td>
                <td><form:errors path="password" cssClass="error"/></td>
            </tr>

            <tr>
                <td>Enter your Name:</td>
                <td><form:input path="studentName"/></td>
                <td><form:errors path="studentName" cssClass="error"/></td>
            </tr>

            <tr>
                <td>Enter Phone No:</td>
                <td><form:input path="phone"/></td>
                <td><form:errors path="phone" cssClass="error"/></td>

            </tr>

            <tr>
                <td>Gender:</td>
                <td><form:radiobutton path="gender" value="Male" label="Male" />
                    <form:radiobutton path="gender" value="Female" label="Female" /></td>
                <td><form:errors path="gender" cssClass="error" /></td>

            </tr>

            <tr>
                <td>Select Technology:</td>
                <td><form:select path="technology">
                        <form:options items="${technologyList}" />
                    </form:select></td>
                <td><form:errors path="technology" cssClass="error" /></td>

            </tr>

            <tr>
                <td>Select city:</td>
                <td><form:select path="city">
                        <form:options items="${citesList}" />
                    </form:select></td>
                <td><form:errors path="city" cssClass="error" /></td>
            </tr>


                <td><input type="submit" value="Register"></td>
            </tr>
        </table>

    </form:form>
<br>
<a href="home">VewAll</a>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
<h3 align="right"><a href="home">Back</a></h3>
    <h2 align="center">${headerMessage}</h2>
    <h3 align="center">You have registered with following Info::::</h3>
    <table align="center" border="5px">

            <tr>
            <td>Student email:</td>
            <td>${student.email}</td>
        </tr>

        <tr>
            <td>Student Name:</td>
            <td>${student.studentName}</td>
        </tr>

        <tr>
            <td>Student Phone:</td>
            <td>${student.phone}</td>
        </tr>

        <tr>
            <td>Student Gender</td>
            <td>${student.gender}</td>
        </tr>

        <tr>
            <td>Selected Technology:</td>
            <td>${student.technology}</td>
        </tr>

        <tr>
            <td>City</td>
            <td>${student.city}</td>
        </tr>
        <tr>
            <td>City</td>
            <td>${student.image}</td>
        </tr>

    </table>

</body>
</html>


package com.infotech.dao;

import com.infotech.model.Student;
import java.util.List;

public interface StudentDAO {
     public void addEmployee(Student student);

        public List<Student> getAllStudent();

        public void deleteStudent(Integer id);

        public Student updateStudent(Student student);

        public Student getStudent(int id);
}

package com.infotech.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

import com.infotech.dao.StudentDAO;
import com.infotech.model.Student;

 @Repository
public class StudentDAOImpl implements StudentDAO {

    @Autowired
    private  SessionFactory sessionFactory;

    @Override
    public void addEmployee(Student student) {
         sessionFactory.getCurrentSession().saveOrUpdate(student);

    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Student> getAllStudent() {
        // TODO Auto-generated method stub

      return sessionFactory.getCurrentSession().createQuery("from Student").list();
    }

    @Override
    public void deleteStudent(Integer id) {
        // TODO Auto-generated method stub
        Student student=(Student) sessionFactory.getCurrentSession().load(Student.class, id);
         if (null != student) {
                this.sessionFactory.getCurrentSession().delete(student);
            }
    }

    @Override
    public Student updateStudent(Student student) {
        sessionFactory.getCurrentSession().update(student);
        return student ;
    }

    @Override
    public Student getStudent(int id) {
        // TODO Auto-generated method stub
        return (Student) sessionFactory.getCurrentSession().get(
                Student.class, id);
    }



}


package com.infotech.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "student_table")
public class Student {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    @NotEmpty
    @Pattern(regexp="[^0-9]+")
    @Size(min=6,max=20)
    @Column(name = "student_name")
    private String studentName;

    @NotEmpty
    @Column(name = "gender")
    private String gender;

    @Size(min=1)
    @NotNull
    @Column(name = "technology")
    private String technology;

    @NotEmpty
    @Column(name = "city")
    private String city;

    @NotEmpty
    @Email
    @Column(name = "email")
    private String email;

    @NotEmpty
    @Column(name = "password")
    private String password;

    @NotNull
    @Min(value = 1000000000)
    @Column(name = "phone")
    private Long phone;


    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }


    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPhone(Long phone) {
        this.phone = phone;
    }

    public Long getPhone() {
        return phone;
    }

    public String getTechnology() {
        return technology;
    }
    public void setTechnology(String technology) {
        this.technology = technology;
    }
}

package com.infotech.service;

import com.infotech.model.Student;
import java.util.List;
public interface StudentService {
    public void addStudent(Student student);

    public List<Student> getAllStudent();

    public void deleteStudent(Integer id);

    public Student getStudent(int id);

    public Student updateEmployee(Student student);

}

package com.infotech.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infotech.dao.StudentDAO;
import com.infotech.model.Student;
import com.infotech.service.StudentService;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDAO studentdao;
    @Override
    public void addStudent(Student student) {
        // TODO Auto-generated method stub
        studentdao.addEmployee(student);

    }

    @Override

    @Transactional
    public List<Student> getAllStudent() {
        // TODO Auto-generated method stub
        return studentdao.getAllStudent();
    }

    @Override

    @Transactional
    public void deleteStudent(Integer id) {
        // TODO Auto-generated method stub
        studentdao.deleteStudent(id);
    }

    @Override
    @Transactional
    public Student getStudent(int id) {
        // TODO Auto-generated method stub
        return studentdao.getStudent(id);
    }

    @Override
    @Transactional
    public Student updateEmployee(Student student) {
        // TODO Auto-generated method stub
        return studentdao.updateStudent(student);
    }


}


package com.infotech.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.servlet.ModelAndView;

import com.infotech.model.Student;

import com.infotech.service.StudentService;

@Controller
public class MyController {

    @Autowired
    private StudentService studentService;







    public static String uploadDirectory = System.getProperty("user.dir")+"/uploads";

    @RequestMapping(value = {"/","/register"} ,method=RequestMethod.GET)
    public String registerPage(Model model){
        model.addAttribute("student", new Student());
        return "register";
    }

    @RequestMapping(value ="/registerSuccess" ,method=RequestMethod.POST)
    public ModelAndView registerSuccess(@Valid @ModelAttribute("student") Student student,BindingResult bindingResult ){
        if(bindingResult.hasErrors()){
            return new ModelAndView("register");
        }
    studentService.addStudent(student);
        ModelAndView modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("student", student);
        return modelAndView;


    }


    @ModelAttribute
    public void headerMessage(Model model){
        model.addAttribute("headerMessage", "Welcome to Starve Technology");

        List<String> techList = new ArrayList<>();
        techList.add("Hibernate");
        techList.add("Spring");
        techList.add("JSP");
        techList.add("Servlet");
        techList.add("Struts");

        List<String> citesList = new ArrayList<>();
        citesList.add("Pune");
        citesList.add("Chennai");
        citesList.add("Delhi");
        citesList.add("Other");

        model.addAttribute("technologyList", techList);
        model.addAttribute("citesList", citesList);
    }
    @RequestMapping(value = "/home" )
    public ModelAndView vewPage(ModelAndView model){
        List<Student> liststudents=studentService.getAllStudent();
         model.addObject("listEmployee", liststudents);
         model.setViewName("home");
        return model;
    }

    @RequestMapping(value = "/editEmployee", method = RequestMethod.GET)
    public ModelAndView editContact(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        Student student = studentService.getStudent(id);
        ModelAndView model = new ModelAndView("register");
        model.addObject("student", student);

        return model;
    }
    @RequestMapping(value = "/deleteEmployee", method = RequestMethod.GET)
    public ModelAndView deleteEmployee(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        studentService.deleteStudent(id);
        return new ModelAndView("redirect:/");
    }


}