[英]No mapping found for HTTP request with URI error when context path is defined
在每個人都說這是騙子之前,我已經閱讀了有關此錯誤的其他問答,但我沒有嘗試過。 至少在上周的某個時候它一直在工作。 今天,我收到此錯誤。 以下是我的文件:
pom.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<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>security</groupId>
<artifactId>password-strength</artifactId>
<version>1.0.0-SNAPSHOT</version>
<url>http://www.xxxx.com</url>
<name>Spring Security - ${project.artifactId}</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.17.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.passay</groupId>
<artifactId>passay</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.2.v20140723</version>
<configuration>
<jvmArgs>-Xmx2048m -Xms512m -XX:MaxPermSize=512m</jvmArgs>
<httpConnector>
<port>9086</port>
</httpConnector>
<stopPort>9968</stopPort>
<stopKey>jetty-stop</stopKey>
<stopWait>10</stopWait>
<useTestScope>true</useTestScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<include>index.html</include>
<targetPath>docs/</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
SRC \\主\\資源\\ META-INF \\彈簧\\一體化\\ applicationContext.xml中:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder />
<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="security"/>
</beans>
SRC \\主\\安全\\網絡\\ Run.java:
package security;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Run {
public static void main(String[] args) {
SpringApplication.run(Run.class, args);
}
}
SRC \\主\\ web應用\\ WEB-INF \\ config目錄\\ web的應用程序-config.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:META-INF/spring/integration/applicationContext.xml"/>
</beans>
SRC \\主\\ web應用\\ WEB-INF \\ web.xml中:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/integration/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
的src / main /安全/網絡/ UserRegistrationController.java:
package security.web;
import security.web.dto.UserRegistrationDto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
@Controller
@RequestMapping("/registration")
public class UserRegistrationController {
@ModelAttribute("user")
public UserRegistrationDto userRegistrationDto() {
return new UserRegistrationDto();
}
@GetMapping
public String showRegistrationForm(Model model) {
return "registration";
}
@PostMapping
public String registerUserAccount(@ModelAttribute("user") @Valid UserRegistrationDto userDto,
BindingResult result) {
if (result.hasErrors()) {
return "registration";
}
return "redirect:/registration?success";
}
}
SRC \\主\\資源\\模板\\ registration.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"/>
<link rel="stylesheet" type="text/css" th:href="@{/css/main.css}"/>
<title>Registration</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-body">
<div class="text-center">
<h3><i class="glyphicon glyphicon-user" style="font-size:2em;"></i></h3>
<h2 class="text-center">Register</h2>
<div class="panel-body">
<div th:if="${param.success}">
<div class="alert alert-info">
You've successfully registered with our awesome app!
</div>
</div>
<form th:action="@{/registration}" th:object="${user}" method="post">
<p class="error-message"
th:if="${#fields.hasGlobalErrors()}"
th:each="error : ${#fields.errors('global')}"
th:text="${error}">Validation error</p>
<div class="form-group"
th:classappend="${#fields.hasErrors('firstName')}? 'has-error':''">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-user color-blue"></i>
</span>
<input id="firstName"
class="form-control"
placeholder="First name"
th:field="*{firstName}"/>
</div>
<p class="error-message"
th:each="error: ${#fields.errors('firstName')}"
th:text="${error}">Validation error</p>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('lastName')}? 'has-error':''">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-user color-blue"></i>
</span>
<input id="lastName"
class="form-control"
placeholder="Last name"
th:field="*{lastName}"/>
</div>
<p class="error-message"
th:each="error: ${#fields.errors('lastName')}"
th:text="${error}">Validation error</p>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('email')}? 'has-error':''">
<div class="input-group">
<span class="input-group-addon">@</span>
<input id="email"
class="form-control"
placeholder="E-mail"
th:field="*{email}"/>
</div>
<p class="error-message"
th:each="error: ${#fields.errors('email')}"
th:text="${error}">Validation error</p>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('confirmEmail')}? 'has-error':''">
<div class="input-group">
<span class="input-group-addon">@</span>
<input id="confirmEmail"
class="form-control"
placeholder="Confirm e-mail"
th:field="*{confirmEmail}"/>
</div>
<p class="error-message"
th:each="error: ${#fields.errors('confirmEmail')}"
th:text="${error}">Validation error</p>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('password')}? 'has-error':''">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-lock"></i>
</span>
<input id="password"
class="form-control"
placeholder="password"
type="password"
th:field="*{password}"/>
</div>
<ul class="text-left"
th:each="error: ${#fields.errors('password')}">
<li th:each="message : ${error.split(',')}">
<p class="error-message"
th:text="${message}"></p>
</li>
</ul>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('confirmPassword')}? 'has-error':''">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-lock"></i>
</span>
<input id="confirmPassword"
class="form-control"
type="password"
placeholder="Confirm password"
th:field="*{confirmPassword}"/>
</div>
<ul class="text-left"
th:each="error: ${#fields.errors('confirmPassword')}">
<li th:each="message : ${error.split(',')}">
<p class="error-message"
th:text="${message}"></p>
</li>
</ul>
</div>
<div class="form-group"
th:classappend="${#fields.hasErrors('terms')}? 'has-error':''">
<input id="terms"
type="checkbox"
th:field="*{terms}"/>
<label class="control-label" for="terms">
I agree with the <a href="#">terms and conditions</a> for Registration.
</label>
<p class="error-message"
th:each="error : ${#fields.errors('terms')}"
th:text="${error}">Validation error</p>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success btn-block">Register</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
Already registered? <a href="/" th:href="@{/login}">Login</a>
</div>
<div class="col-md-12">
Forgot password? <a href="/" th:href="@{/forgot-password}">Reset password</a>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" th:src="@{/webjars/jquery/3.2.1/jquery.min.js/}"></script>
<script type="text/javascript" th:src="@{/webjars/bootstrap/3.3.7/js/bootstrap.min.js}"></script>
</body>
</html>
我加載:
http://localhost:9086/registration/
並得到:
HTTP ERROR 404
Problem accessing /registration/. Reason:
Not Found
Powered by Jetty://
有任何想法嗎? 我嘗試了各種方法,通讀了一些教程,並在各種站點上嘗試了diff方法。 到目前為止沒有運氣。 在我看來應該可以使用,但是...
更新:
我在一些文件中恢復了以前的狀態(更新了上面的那些文件)。 我發現正在發生以下情況:順便說一下,我正在使用IntelliJ IDEA社區版。 當我在本地構建並運行時
mvn clean compile
該頁面無法加載(未找到映射,上面報告了異常)。
但是,當我直接從Maven項目下運行Lifecycle \\ compile並將Jetty設置為通過Plugins \\ jetty:run運行時,頁面將成功加載。 此外,我可能還需要在IntelliJ中將root \\ src \\ main標記為Sources Root或取消標記,然后再次將其標記為,再次運行Lifecycle \\ compile,然后在啟動Jetty時成功加載頁面。 否則,我會得到映射錯誤。
這告訴我,成功或失敗在某種程度上取決於項目的構建方式,即通過何種工具。 同樣,這似乎暗示了工具狀態依賴性與構建問題本身。 這令人沮喪,並使其成為各種Heisenbug。 這不好。
我還將正在談論的頁面(registration.html)添加到OP中的文件列表中。
在哪里設置上下文路徑? 例如application.properties?
server.servlet.context-path=/my-context-path
實際上,關於Spring Boot,設置上下文路徑后,要通過HTTP Request訪問應用程序,必須在URL前面加上上下文路徑:
http://localhost:9086/my-context-path/registration
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.