gpt4 book ai didi

java - Tomcat 服务器已启动,但未从 Controller 访问 URL

转载 作者:行者123 更新时间:2023-12-02 11:36:03 26 4
gpt4 key购买 nike

我开发了一个示例 Maven 项目(引用 link )。最后,它不是按预期运行。

这是Project Structure .

下面是代码:
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Counter Web Application</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>cleanTempDirectory</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

mvc-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: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.mkyong.controller" />

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

welcome.jsp

<h1>Welcome..!!!</h1>
<a href="/SampleProj/home.html">click</a>
<%-- <%request.getRequestDispatcher("/home.html").forward(request, response);%> --%>

home.jsp

<html>
<body>
<h1>Maven + Spring MVC Web Project Example</h1>
</body>

BaseController.java

@Controller
public class BaseController {

private static final String VIEW_INDEX = "home";

@RequestMapping(value = "home.html")
public String welcome(ModelMap model) {

model.addAttribute("message", "Welcome");

// Spring uses InternalResourceViewResolver and return back index.jsp
return VIEW_INDEX;

}
}

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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.java</groupId>
<artifactId>SampleProj</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SampleProj Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.8</jdk.version>
<spring.version>4.2.6.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.11</junit.version>
<logback.version>1.0.13</logback.version>
<jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
</properties>

<dependencies>

<!-- Unit Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>

<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jcl-over-slf4j.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

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

</dependencies>

<build>
<finalName>SampleProj</finalName>

<plugins>
<!-- Eclipse project -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<!-- Always download and attach dependencies source code -->
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
<!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 -->
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>

<!-- Set JDK Compiler Level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>

<!-- For Maven Tomcat Plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat8-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/SampleProj</path>
</configuration>
</plugin>

</plugins>

</build>
</project>

输出:
welcome page .
点击后.,, error page

我尝试了所有可能的情况,但根本没有用。但是来自另一个 eclipse 版本和另一个工作区(相同 tomcat 版本)的其他项目运行良好。我在没有 maven 的情况下尝试了相同的项目(从构建路径添加 jars),它的工作。对于 Maven 来说,它没有按预期发生。我认为,pom 文件被中断了流程。是否需要更改 eclipse 设置?(Show View(Problems) 中没有错误)。建议我..

最佳答案

我认为部署程序集中缺少 Maven 依赖项。因此,将 Maven 依赖项添加到您的部署程序集中。

项目->属性->部署程序集->添加->Java 构建路径条目->Maven 依赖项。

完成上述过程后,重新启动您的 tomcat 服务器。

关于java - Tomcat 服务器已启动,但未从 Controller 访问 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48929330/

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