Java 为什么我看不到我的网站,并获得 HTTP 状态 404?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18641245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:46:35  来源:igfitidea点击:

Why can't I see my site, and get HTTP Status 404?

javaspringmodel-view-controllerhttp-status-code-404status

提问by vardius

I'm doing servlet using spring mvc and I can't see my site. I've posted my web.xml, mvc-dispatcher-servlet.xml and controller class below. Everything seems to be fine, but when I enter "localhost:8080" I get 404 http status The requested resource is not available. Did I miss something?

我正在使用 spring mvc 做 servlet,但我看不到我的网站。我已经在下面发布了我的 web.xml、mvc-dispatcher-servlet.xml 和控制器类。一切似乎都很好,但是当我输入“localhost:8080”时,我得到404 http status The requested resource is not available. 我错过了什么?

web.xml:

网页.xml:

<web-app 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>Weather</display-name>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

mvc-dispatcher-servlet.xml:

mvc-dispatcher-servlet.xml:

<context:component-scan base-package="com.springapp.mvc"/>

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

<jpa:repositories base-package="com.springapp.mvc"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="defaultPersistenceUnit"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>
<mvc:annotation-driven />

Controller class:

控制器类:

@Controller
public class HomeController
{
    Weather weather;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String infoWeather(ModelMap model, HttpServletRequest req) {

        if (!model.containsAttribute("location") && !model.containsAttribute("weather")) {
            Language.getInstance().setLanguage(LanguageManager.getLanguage(new Locale(System.getProperty("user.language"))));
            Location location = LocationManager.getLocation(req);
            model.addAttribute("location", location);
            weather = WeatherManager.getWeather(location);
            model.addAttribute("weather", weather);
        }

        if (!model.containsAttribute("destination"))
            model.addAttribute("destination", new Destination());

        return "index";
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String findDestination(@ModelAttribute("destination") Destination destination, BindingResult result, RedirectAttributes redirectAttrs) {
        destination = DestinationManager.getDestination(destination.getAddress());
        weather = WeatherManager.getWeather(destination);
        redirectAttrs.addFlashAttribute("weather", weather);
        return "redirect:/";
    }
}

采纳答案by Gyanendra Dwivedi

I see below points here, which needs to be fixed:

我在这里看到以下几点,需要修复:

  1. Once you deploy your application in tomcat webapp directory, the URL should be something like http://localhost:8080/<web-app-name>/<your servlet>

  2. Alternatively you could add a welcome page and check if it is working. For that you need to add welcome page setting in web-xml. See This postfor a clue.

  1. 在 tomcat webapp 目录中部署应用程序后,URL 应该类似于 http://localhost:8080/<web-app-name>/<your servlet>

  2. 或者,您可以添加一个欢迎页面并检查它是否正常工作。为此,您需要在 web-xml 中添加欢迎页面设置。请参阅此帖子以获取线索。

If this is not working, please share your tomcat directory tree with your war deployed; and the war file directory structure

如果这不起作用,请与部署的战争共享您的 tomcat 目录树;和war文件目录结构

Note: you should be able to expand your war using a zip utility.

注意:您应该能够使用 zip 实用程序扩展您的战争。