2

我在做使用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:/"; 
    } 
} 
+0

很可能你的应用程序在你的servlet容器上有一个上下文路径。所以你不会在'localhost:8080 /'上访问它,而是在'localhost:8080/that-path'上访问它。 –

+0

我该如何检查? – Vardius

+0

intellij想法它是 – Vardius

回答

1

我见下文点在这里,这需要待修:

  1. 一旦你部署在tomcat的webapp目录下你的应用程序中,URL应该像http://localhost:8080/<web-app-name>/<your servlet>

  2. 或者你可以添加一个欢迎页面,并检查其是否工作。为此,您需要在web-xml中添加欢迎页面设置。有关线索,请参见This post

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

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

+0

这里是war文件'E:\ IntelliJ Projects \ What2Wear \ target' 这里是tomcat :'E:\ IntelliJ Projects \ apache-tomcat-7.0.37' http://oi44.tinypic.com/3039qo5.jpg – Vardius

+0

你有没有在tomcat中部署你的战争并重新启动tomcat?您的战争位于工作区的目标位置,但您需要将其添加到Tomcat服务器。如果在工作区内配置了tomcat服务器,则可以尝试查找添加/删除模块类型的菜单。否则,从工作位置复制战争,粘贴到tomcat的web应用程序位置,然后手动启动服务器。另外,在服务器启动时查找日志,战争部署应该是成功的,以访问您的网页和servlet。 – gyan

+0

我的服务器日志: '[2013-09-05 06:50:30,869]神器What2Wear_SpringMVC:战争爆炸:神器正在部署中,请稍等... wrz 05,2013 6:50:31 PM org.apache。 catalina.loader.WebappClassLoader validateJarFile INFO:validateJarFile(E:\ IntelliJ Projects \ What2Wear \ target \ What2Wear \ WEB-INF \ lib \ servlet-api-2.5.jar) - jar未加载。请参阅Servlet规范2.3,第9.7.2节。违规类:javax/servlet/Servlet。类 [2013-09-05 06:50:32,364]神器What2Wear_SpringMVC:战争爆炸:神器部署成功' – Vardius

相关问题