gpt4 book ai didi

Spring MVC : HTTP status 404: The requested resource is not available

转载 作者:行者123 更新时间:2023-12-02 03:34:28 25 4
gpt4 key购买 nike

我是 Spring MVC 的新手。我正在尝试将 Thymeleaf 与 Spring 4 集成,并且我用一个 Controller /方法编写了一个 helloworld 程序。但是,当我启动Tomcat时,在访问http://localhost:8080/hello

时出现了这个错误
HTTP Status 404 -

type Status report

message

description The requested resource is not available.

在我的 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>homeMadeSpring</groupId>
<artifactId>homeMadeSpringFirst</artifactId>
<version>1.0-SNAPSHOT</version>
<name>homeMadeSpringFirst</name>
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.0.5.RELEASE</spring.version>
<spring.security.version>3.2.4.RELEASE</spring.security.version>
</properties>

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

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>2.1.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<finalName>homeMadeSpringFirst</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.2.v20120308</version>
<configuration>
<webAppConfig>
<contextPath>/homemade</contextPath>
</webAppConfig>
<scanIntervalSeconds>1</scanIntervalSeconds>
<stopKey>stop-jetty</stopKey>
<stopPort>9999</stopPort>
</configuration>
</plugin>
</plugins>
</build>s
</project>

在我的 src/main/resources 目录中,我有两个文件:applicationContext-security.xml 和 thymeleaf-pages.xml。

applicationContext-security.xml的内容是:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<http pattern="/login" security="none"/>
<http pattern="/assets/**" security="none"/>

<http auto-config="true" use-expressions="true">
<intercept-url pattern="/homeMadeSpring" access="isAuthenticated()" />

<form-login
login-processing-url="/authentication"
login-page="/login"
authentication-failure-url="/login?error=true"
default-target-url="/hello"
always-use-default-target="false"
/>
<remember-me user-service-ref="userDetailsService"/>
<logout logout-url="/logout"
logout-success-url="/login"/>
</http>

<authentication-manager>
<authentication-provider user-service-ref="userDetailsService"/>
</authentication-manager>


<user-service id="userDetailsService">
<user name="admin" password="admin" authorities="ROLE_USER"/>
</user-service>

</beans:beans>

**在我的 thyme-pages.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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">


<mvc:resources location="/assets/img/" mapping="/assets/img/**" />
<mvc:resources location="/assets/css/" mapping="/assets/css/**" />
<mvc:resources location="/assets/js/" mapping="/assets/js/**" />


<bean id="httpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>

<bean id="defaultMessageConverters" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="httpMessageConverter"/>
</list>
</constructor-arg>
</bean>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
<property name="messageConverters" ref="defaultMessageConverters"/>
<property name="cacheSeconds" value="0"/> <!-- NO CACHE -->
</bean>


<bean id="homeController" class="controllers.HomeController"/>


<bean id="contentNegotiatingResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="pdf" value="application/pdf"/>
<entry key="xsl" value="application/vnd.ms-excel"/>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json"/>
</map>
</property>
</bean>

<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false"/>
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>


<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8"/>
</bean>

<bean id="dateFormatter" class="util.DateFormatter"/>



</beans>

在 src/main/webapp/WEB-INF 目录中,我的 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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:thymeleaf-pages.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/pages/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<servlet-name>HelloWorld</servlet-name>
</filter-mapping>
</web-app>

最后,我的 Controller 是:

@Controller
public class HomeController
{
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String helloWorld(Model model)
{
return "pages/hello";
}
}

我的项目结构截图:

enter image description here

最佳答案

这里有两件事:

1) 您已经在 pom.xml 中使用上下文名称配置了 jetty 插件:homemade,因此如果您使用的是 jetty,则必须使用此上下文。

2) 您已经配置了 spring security,但是您没有任何带有 @RequestMapping(value = "/login") 的 Controller ,要么删除 spring security,要么在 Controller 中定义一个方法来处理登录页面.

关于 Spring MVC : HTTP status 404: The requested resource is not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24566327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com