我正在使用 spring mvc 做 servlet,但我看不到我的站点。我在下面发布了我的 web.xml、mvc-dispatcher-servlet.xml 和 Controller 类。一切似乎都很好,但是当我输入“localhost:8080”时,我得到了 404 http status
请求的资源不可用
。我错过了什么吗?
web.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:
<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 类:
@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:/";
}
}
最佳答案
我在这里看到以下几点,需要修复:
将应用程序部署到 tomcat webapp 目录后,URL 应该类似于
http://localhost:8080/<web-app-name>/<your servlet>
。或者,您可以添加一个欢迎页面并检查它是否正常工作。为此,您需要在 web-xml 中添加欢迎页面设置。参见 This post寻找线索。
如果这不起作用,请与部署的 war 共享您的 tomcat 目录树;和war文件目录结构
注意:您应该能够使用 zip 实用程序扩展您的 war 。
关于java - 为什么我看不到我的网站,也看不到 HTTP 状态 404?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18641245/