2012-12-11 46 views
1

這是我的第一篇文章,所以對我來說很容易。我試圖在JBoss AS 7和TomCat 7上使用spring web mvc 3.1.3建立一個java web應用程序。但是在URL映射中發生了一些奇怪的事情。我正在關注一些教程,但我基本上正在嘗試執行this one的第一部分。在我的WEB-INF/views文件夾中有一個非常簡單的jsp,我想在調用http://localhost:8080/hello/welcome時得到渲染並返回。但是這並沒有發生。 URL映射有問題。當我使用/ test/*之類的東西時,我打電話給http://localhost:8080/hello/test/welcome,它按預期工作。當我使用/ * http://localhost:8080/hello/welcome時返回一個非呈現的jsp。當我使用我的意見應該是默認情況下,我會得到404資源未找到。 我在eclipse中運行的JBoss 7.1.1和Tomcat 7.0.33服務器上測試了這一切,或者使用war文件進行了部署。我不知道該怎麼做。每個谷歌結果頁面都充滿了紫色鏈接,我仍然沒有找到我要找的東西。沒有xml配置奇怪的URL映射web應用程序

任何人都可以幫忙嗎?我瞭解一些信息可能會丟失,但請詢問。

編輯:我忘了我使用maven來構建我的應用程序。這是使用用於蝕食的m2e wtp插件完成的。我正在運行eclipse juno。

這是我的初始化程序(非常基本)。

Public class Initializer implements WebApplicationInitializer 
{ 
public void onStartup(ServletContext servletContext) throws ServletException { 
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); 
root.register(WebappConfig.class); 

ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(root)); 
servlet.addMapping("/"); 
servlet.setLoadOnStartup(1); 
}} 

這是我WebappConfig:

@Configuration 
@ComponentScan("nl.hello") 
@EnableWebMvc 
public class WebappConfig extends WebMvcConfigurerAdapter 
{ 
@Bean 
public InternalResourceViewResolver setupViewResolver() 
{ 
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
resolver.setPrefix("/WEB-INF/views/"); 
resolver.setSuffix(".jsp"); 

return resolver; 
} 

@Override 
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer{ 
configurer.enable(); 
}} 

和控制器:

@Controller 
public class HelloController { 

@RequestMapping("/welcome") 
public String helloWorld(Model model) { 
//let’s pass some variables to the view script 
model.addAttribute("wisdom", "Goodbye XML"); 

return "welcome"; 
}} 

我的POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>hello</groupId> 
<artifactId>hello</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>war</packaging> 
<build> 
    <plugins> 
    <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>2.3.2</version> 
     <configuration> 
     <source>1.6</source> 
     <target>1.6</target> 
     </configuration> 
    </plugin> 
    <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.3</version> 
     <configuration> 
     <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
    </plugin> 
    </plugins> 
</build> 
<properties> 
    <project.build.sourceEncoding>utf8</project.build.sourceEncoding> 
</properties> 
<dependencies> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>4.1.8.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>3.1.3.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>3.1.3.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
    <groupId>javax.inject</groupId> 
    <artifactId>javax.inject</artifactId> 
    <version>1</version> 
</dependency> 
<dependency> 
    <groupId>cglib</groupId> 
    <artifactId>cglib</artifactId> 
    <version>2.2.2</version> 
</dependency> 
</dependencies> 
</project> 

回答

1

我解決它!我應該提到,我知道7.0.15之前的tomcat的「bug」。在此版本下面,無法覆蓋默認的網址映射。但正如我所說我知道這一點,我使用的版本7.0.33。所以這不可能是正確的!?錯誤! 在附註中,我應該注意到在jboss的404頁面中,7.1.1(brontes)使用7.0.13版本或者其他東西,並且永遠不會工作。 但問題是什麼。那麼我正在使用m2e wtp plugin進行eclipse構建我的應用程序。不幸的是,maven中有一件事打破了一切。首先,我認爲這是它嵌入的事實,並使用外部maven源解決了我的問題。也許是版本差異?事實證明,他們都使用3.0.4。奇怪的!?當我切換回嵌入式工作。

摘要:使用webapplicationinitializer時,請使用7.0.15的Tomcat版本,最好使用外部maven源代碼。