這是我的第一篇文章,所以對我來說很容易。我試圖在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>