无法在Spring中运行基于表单的简单应用程序

时间:2018-11-29 15:04:01

标签: http-status-code-404

我正在尝试从Spring运行基于小型表单的应用程序。我正在运行Java版本8,spring 4和tomcat 9。 详细信息如下:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.crud</groupId>
    <artifactId>crudOperations</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>crudOperations</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javaee.version>8.0</javaee.version>
        <spring.version>4.3.6.RELEASE</spring.version>
        <jstl.version>1.2</jstl.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- JAVA EE -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

    </dependencies>

    <build>
        <!-- Updated for the latest version of JAVA -->
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

        <finalName>crudOperations</finalName>
    </build>
</project>

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>Archetype Created Web Application</display-name>
    <welcome-file-list>
    <welcome-file>home.jsp</welcome-file>
        <welcome-file>home.html</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>success.jsp</welcome-file>
    </welcome-file-list>

    <!-- Configuring front-controller -->
    <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>

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

    ">

    <context:component-scan base-package="com.crud.Controllers"/>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />

    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>



</beans>

HomeController.java

package com.crud.Controllers;

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

@Controller
public class HomeController {

    @RequestMapping(value = {"/", "/home"})
    public ModelAndView goToHomePage() {
        ModelAndView view = new ModelAndView();
        view.setViewName("home");

        return view;
    }


    @RequestMapping(value = "/FormSubmit")
    public ModelAndView goToSubmitSuccessPage() {
        ModelAndView view = new ModelAndView();
        view.setViewName("success");

        return view;
    }

}

home.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>Home page for registration</title>
<form action="FormSubmit" method="post">
User Name: <input type="textfield" name="username" /><br/><br/>
Gender: <input type="radio" name="gender" value="Male" checked/> Male
<input type="radio" name="gender" value="Female"/>Female<br/><br/>
Select a country: <select name="country">
<option>India</option>
<option>US</option>
<option>UK</option>
<option>Ireland</option>
</select>
<br/><br/>
About you: <br/><textarea rows="9" cols="70" name="aboutYou">  </textarea><br/>
Would you like to join our mailing list?<input type="checkbox" name="mailList"><br/><br/>
<input type="submit" value="submit"/>
</form>
</head>
</html>

success.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>
<h3> You have submitted you application successfully</h3>
</body>
</html>

第一次尝试运行该程序时,URL http://localhost:8080/crudOperations/可以正常打开,但是在尝试访问http://localhost:8080/crudOperations/FormSubmit时却出现以下错误: HTTP状态404 –找不到 类型状态报告

描述原始服务器找不到目标资源的当前表示,或者不愿意透露该资源的存在。 Apache Tomcat / 9.0.8

我尝试了SO中建议的所有替代方法。我已经更改了部署程序集并在其中添加了视图文件夹,我也从部署程序集中添加了maven依赖关系,但仍然无法正常工作。

在我被困在这里时,任何帮助将不胜感激。

非常感谢!

0 个答案:

没有答案