没有xml配置奇怪的url映射web应用程序

时间:2012-12-11 11:32:04

标签: web-applications spring-mvc tomcat7 jboss7.x url-mapping

这是我的第一篇文章,所以对我很轻松。我正在尝试使用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时将其呈现并返回。但这不会发生。网址映射有问题。当我使用像/ test / *之类的东西时,我调用http://localhost:8080/hello/test/welcome它会按预期工作。当我使用/ * http://localhost:8080/hello/welcome返回一个非渲染的jsp时。当我使用我在noob意见中的意思应该是默认的时候我找不到404资源。 我在JBoss 7.1.1和在eclipse中运行的Tomcat 7.0.33服务器上测试了这一切,或者使用war文件进行了部署。我没办法。每个谷歌结果页面都充满了紫色链接我仍然没有找到我正在寻找的东西。

有谁可以提供帮助?我知道有些信息可能会丢失,但请问。

编辑:我忘记了我使用maven来构建我的应用程序。这是使用eclipse的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 个答案:

答案 0 :(得分:1)

我解决了!我应该提到我知道版本7.0.15之前的tomcat的“bug”。在此版本下面,无法覆盖默认的URL映射。但正如我所说,我知道它,我使用的是7.0.33版本。所以这不可能是正确的!?错误! 在旁注上我应该注意到jboss的404页面中7.1.1(brontes)使用版本7.0.13或其他东西并且永远不会起作用。 但是问题是什么。我正在使用m2e wtp插件为eclipse构建我的应用程序。不幸的是maven中有一些东西打破了一切。首先,我认为它是嵌入的事实,使用外部maven源确实解决了我的问题。也许是版本差异?事实证明他们都使用3.0.4。奇怪的!?当我切换回嵌入式时,它起作用了。

摘要:使用webapplicationinitializer时,请使用Tomcat版本的7.0.15,最好使用外部maven源。