Java HTTP状态404-/EventTracker/greeting
我正在尝试访问我机器上的。然而,我得到一个404错误。我正在关注SpringMVC4教程的PluralSight介绍,我的代码似乎与视频中的代码相匹配。我正在使用两个java文件WebConfig和WebAppInitializer来配置我的应用程序。我遗漏了什么吗?我想我已经一行一行地抄写了,但仍然没有工作 HelloController.javaJava HTTP状态404-/EventTracker/greeting,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在尝试访问我机器上的。然而,我得到一个404错误。我正在关注SpringMVC4教程的PluralSight介绍,我的代码似乎与视频中的代码相匹配。我正在使用两个java文件WebConfig和WebAppInitializer来配置我的应用程序。我遗漏了什么吗?我想我已经一行一行地抄写了,但仍然没有工作 HelloController.java @Controller public class HelloController { @RequestMapping(value="/g
@Controller
public class HelloController {
@RequestMapping(value="/greeting")
public String sayHello(Model model) {
model.addAttribute("greeting", "Hello World");
return "hello.jsp";
}
}
WebAppInitializer.java
public class WebAppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context) );
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context) );
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.html");
}
private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.pluralsight.WebConfig");
return context;
}
}
WebConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.pluralsight")
public class WebConfig {
}
hello.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>
<h1>${greeting}</h1>
</body>
</html>
您的url
http://localhost:8080/EventTracker/greeting
与您的dispatcher映射不匹配:dispatcher.addMapping(“*.html”)代码>。试试http://localhost:8080/EventTracker/greeting.html
我从未创建过没有XML配置文件的Spring MVC应用程序。我相信这是可能的,但我必须调查一下。如果您不介意使用XML文件进行配置,可以执行类似于以下的操作(这是我不久前为更好地熟悉Spring MVC而做的一个小项目):
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>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--if not using jsp, can omit this -->
<jsp-config> <!-- if taglib not inside jsp-config, will cause deployment errors -->
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/taglib/c.tld</taglib-location>
</taglib>
</jsp-config>
Spring3MVC
index.jsp
春天
org.springframework.web.servlet.DispatcherServlet
1.
春天
*.html
http://java.sun.com/jsp/jstl/core
/WEB-INF/taglib/c.tld
xml是配置的顶层。从上面可以看出,需要记住的重要一点是,哪个文件(它也是一个XML文件)的名称是DispatcherServlet文件。标记中包含的任何内容都将附加-servlet.xml,因此在本例中,我的dispatcher servlet将是一个名为spring-servlet.xml的文件。标记告诉应用程序与DispatcherServlet关联的url模式类型。因此,在本例中,任何以.html结尾的url都将由spring-servlet.xml处理
如果您使用的是JSP,请确保所有标记都在一个标记内,否则它将无法正常工作
springservlet.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:context="http://www.springframework.org/schema/context"
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:component-scan
base-package="net.viralpatel.spring3.controller" />
<!-- declares package where controller(s) stored. Don't need to declare indvd controllers -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" /> <!-- hello.jsp must be located in this directory for link to work -->
<property name="suffix" value=".jsp" />
</bean>
这就是dispatcher servlet文件的外观。您可以忽略顶部的代码。根据您发布的错误,我猜测您的java代码遗漏了视图解析器或映射错误。视图解析器将传递到控制器(即:hello)的字符串转换为相对URL路径(即:/WEB-INF/jsp/hello.jsp)。要使其正常工作,请确保所有jsp文件都位于同一目录中,并将该目录作为前缀值的一部分列出。在本例中,我将所有jsp文件存储在WEB-INF目录中名为jsp的目录中。本例中的后缀只是文件扩展名。任何未存储在此目录中的文件都会导致应用程序在尝试加载丢失的文件时抛出404错误
我知道这并不完全是您打算做的,但如果您选择使用XML文件,我希望这会有所帮助。如果您有任何问题,请告诉我。因为这是我在谷歌上找到的第一个问题,这里没有正确的答案,这就是帮助我的原因
下一步应该添加WebAppInitializer.java
context.register(com.pluralsight.WebConfig.class);
因此,您的文件应该如下所示:
public class WebAppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context) );
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context) );
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.html");
}
private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.pluralsight.WebConfig");
context.register(com.pluralsight.WebConfig.class);
return context;
}
}
我也有同样的问题。
问题是我的Tomcat安装文件夹中没有“本机库”。
我用以下方法解决这个问题:
sudo apt get安装libtcnative-1
然后我遇到了我的本机库版本太旧的问题,我通过升级解决了这个问题:
sudo apt获得升级libtcnative-1
我希望这会有所帮助:)
在webapp Initializer中添加此选项将解决您的问题能否发布DispatcherServlet.xml代码?您的错误似乎与您的应用程序在DispatcherServlet文件中找不到正确的映射有关。我没有任何可用于配置的xml文件。所有配置都由java文件完成如果您决定使用xml文件,我已经发布了一个答案,它将帮助您解决404错误。我不确定是否只使用java文件代码>到dispatcher.addMapping(“/*”);上面的变化没有解决任何问题。使用更改,我甚至不允许访问我的索引页。必须将此标记为正确答案!这是我的工作
public class WebAppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context) );
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context) );
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("*.html");
}
private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.pluralsight.WebConfig");
context.register(com.pluralsight.WebConfig.class);
return context;
}
}
context.register(WebConfig.class)