我在做使用spring mvc的servlet,我看不到我的網站。我在下面發佈了我的web.xml,mvc-dispatcher-servlet.xml和控制器類。一切似乎都很好,但是當我輸入「localhost:8080」時,我得到了404 http status The requested resource is not available
。我錯過了什麼?爲什麼我看不到我的網站,並獲得HTTP狀態404?
的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-調度-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
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:/";
}
}
很可能你的應用程序在你的servlet容器上有一個上下文路徑。所以你不會在'localhost:8080 /'上訪問它,而是在'localhost:8080/that-path'上訪問它。 –
我該如何檢查? – Vardius
intellij想法它是 – Vardius