0

I'm aware similar questions have been asked but I can't seem to find one that is the same as my configuration.

I have an Eclipse Java EE project set up with Maven and spring. I followed these tutorials to set it up: http://fruzenshtein.com/setup-of-dynamic-web-project-using-maven/ http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/

When I launch the project I get an error 404.

The tutorials didn't mention the creation of a dispatcher-servlet.xml so maybe that is the problem but I'm not sure if I even need it with Maven.

My config is as follows:

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FindLove</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

pom.xml

<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>FindLove</groupId>
  <artifactId>FindLove</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>  
    <spring.version>3.1.1.RELEASE</spring.version>  
  </properties> 
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
                    <source>${jdk.version}</source>  
                    <target>${jdk.version}</target> 
        </configuration>
      </plugin> 
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-webmvc</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-web</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <!-- CGLIB is required to process @Configuration classes -->  
    <dependency>  
        <groupId>cglib</groupId>  
        <artifactId>cglib</artifactId>  
        <version>2.2.2</version>  
    </dependency>  
    <!-- Servlet API, JSTL -->  
    <dependency>  
        <groupId>javax.servlet</groupId>  
        <artifactId>javax.servlet-api</artifactId>  
        <version>3.0.1</version>  
        <scope>provided</scope>  
    </dependency>  
    <dependency>  
        <groupId>jstl</groupId>  
        <artifactId>jstl</artifactId>  
        <version>1.2</version>  
    </dependency> 
</dependencies>  
</project>

Configuration class

@Configuration //Specifies the class as configuration  
@ComponentScan("com.sprmvc") //Specifies which package to scan  
@EnableWebMvc //Enables to use Spring's annotations in the code  
public class WebAppConfig {

    @Bean  
    public UrlBasedViewResolver setupViewResolver() {  
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();  
        resolver.setPrefix("/WEB-INF/pages/");  
        resolver.setSuffix(".jsp");  
        resolver.setViewClass(JstlView.class);  
        return resolver;  
    }  

}

Initializer class

public class Initializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  
        ctx.register(WebAppConfig.class);  

        ctx.setServletContext(servletContext);    

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

}

Controller

@Controller
public class LinkController {

    @RequestMapping(value="/hello-page")  
    public ModelAndView goToHelloPage() {  
        ModelAndView view = new ModelAndView();  
        view.setViewName("hello"); //name of the jsp-file in the "page" folder  

        String str = "MVC Spring is here!";  
        view.addObject("message", str); //adding of str object as "message" parameter  

        return view;  
    }  

}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<h1>Home page</h1> 
<p>This is a Home Page.</p>  
<p><a href="hello-page.html">Hello world link</a></p> 

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<p>Hello world: ${message}</p>  
<p>Well done!</p>   

Any advice?

Thanks in advance

4

1 回答 1

0

我不认为

<p><a href="hello-page.html">Hello world link</a></p> 

可以达到Servlet映射到

servlet.addMapping("/");  

并调用@Controller映射到的处理程序方法

@RequestMapping(value="/hello-page")  

将您的链接更改为

<p><a href="hello-page">Hello world link</a></p> 

理想情况下,你会想把它放到

<p><a href="<c:url value="/hello-page" />">Hello world link</a></p> 

使其与上下文路径相关。不要忘记添加标签库声明。

于 2013-10-28T13:35:18.240 回答