簡體   English   中英

Spring MVC 404找不到錯誤

[英]Spring MVC 404 not found error

我試圖在Spring MVC中運行一個項目。 這是代碼

的index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/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=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>

    <body>
        <h1>Spring 3 Register!</h1>
        <a href="register.htm">click</a>
        <form:form action="${pageContext.request.contextPath}/register.htm" method="POST" modelAttribute="userForm">
            <table>
                <tr>
                    <td colspan="2" align="center">Spring MVC Form Demo - Registration</td>
                </tr>
                <tr>
                    <td>User Name</td>
                    <td><form:input path="username" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><form:password path="password" /></td>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><form:input path="email" /></td>
                </tr>
                <tr>
                    <td>BirthDate (mm/dd/yyyy)</td>
                    <td><form:input path="birthDate" /></td>
                </tr>
                <tr>
                    <td>Profession</td>
                    <td><form:select path="profession" items="${professionList}" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Register" /></td>
                </tr>

            </table>
        </form:form>
    </body>
</html>

RegistrationController.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package RegisterInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Harshit Shrivastava
 */
import RegisterInfo.model.User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;

@Controller
@RequestMapping(value = "/register")
public class RegistrationController {

    @RequestMapping(method = RequestMethod.GET)
    public String viewRegistration(Model model)
    {
        User userForm = new User();
                model.addAttribute("userForm", new User());

                /*List<String> professionList = new ArrayList();
                professionList.add("Developer");
                professionList.add("Designer");
                professionList.add("IT Manager");
                model.put("professionList", professionList);*/
        return "index";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processRegistration(@ModelAttribute("userForm") User user, Map<String, Object> model)
    {       
        System.out.println("Username : " + user.getUserName());
        model.put("userForm", new User());
        return "index";
    }
}

User.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package RegisterInfo.model;

/**
 *
 * @author Harshit Shrivastava
 */
import java.util.Date;

public class User {
    private String username;
    private String password;
    private String email;
    private Date birthDate;
    private String profession;

    public String getUserName()
    {
        return username;
    }
    public void setUserName(String username)
    {
        this.username = username;
    }
        public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
        public String getEmail()
    {
        return email;
    }
    public void setEmail(String email)
    {
        this.email = email;
    }
        public Date getBirthDate()
    {
        return birthDate;
    }
    public void setBirthDate(Date birthDate)
    {
        this.birthDate = birthDate;
    }
        public String getProfession()
    {
        return profession;
    }
    public void setProfession(String profession)
    {
        this.profession = profession;
    }
}

web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
</web-app>

調度員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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <context:component-scan base-package="SpringRegister" />
    <mvc:annotation-driven />
    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

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

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

項目結構:

在此處輸入圖片說明

在上面的程序中,我總是得到404 not found status 我試過了

HTTP://本地主機:8080 / RegisterInfoMaven /索引

HTTP://本地主機:8080 / RegisterInfoMaven / index.htm的

兩者都不起作用。

當我從項目中刪除web.xml&dispatcher-servlet.xml時, http://localhost:8080/RegisterInfoMaven/index.jsp加載正常,但是當我放入web.xml & dispatcher-servlet.xml ,它又一次開始給出404 not found錯誤。

web.xml & dispatcher-servlet.xml文件有什么問題?

第一。 您的web.xml文件中的映射:

<servlet-mapping>
   <servlet-name>dispatcher</servlet-name>
   <url-pattern>*.html</url-pattern>
</servlet-mapping>

意味着,調度程序Servlet將僅處理以.html結尾的請求。 將其更改為下一個:

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

之后,調度程序servlet將處理所有傳入的請求。

第二。 來自您的dispatcher-servlet.xml解析器聲明:

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

意味着,當Controller的方法返回String值時,解析器將在任何值之前添加/WEB-INF/jsp並在其后添加.jsp 然后將使用該名稱查找文件。 因此,您需要在WEB-INF目錄中創建jsp目錄,並在其中放置jsp文件。

第三。 當您輸入類似http:// localhost:8080 / index的內容時 ,spring會嘗試查找標記有@RequestMapping(value = "/index") Controller 類或方法 ,而不是找到文件 index.jsp。

因此,您必須聲明標有@RequestMapping(value = "/index") Controller方法。 您的請求字符串將看起來像http:// localhost:8080 / index這樣的東西,沒有任何擴展名。

嘗試像這樣在web.xml寫入您的網址:

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*.htm</url-pattern>

和:

    <welcome-file-list>
    <welcome-file>/index.htm</welcome-file>
   </welcome-file-list>

並嘗試替換為:

  <form:form action="${pageContext.request.contextPath}/register.htm" method="POST" modelAttribute="userForm"

通過此發布,正確的路徑${pageContext.request.contextPath}/register

  <form:form action="${pageContext.request.contextPath}/register" method="POST" modelAttribute="userForm">

並添加值User userForm代替User user如下所示:

   public String processRegistration(@ModelAttribute("userForm") User userForm, Map<String, Object> model)

並更改base-package例如:

 <context:component-scan base-package="RegisterInfo.controller" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM