基于Spring MVC注释的配置无法正常工作

时间:2016-01-27 18:53:59

标签: java spring jsp spring-mvc

我尝试在this tutorial的帮助下创建一个简单的Spring MVC项目 但是当我部署它时显示404服务器错误。因为我是初学者,因为我是初学者,所以我无法弄清楚项目中有什么问题,尽管在这里和其他地方都要经过所有的答案。我将非常感谢你的帮助搞清楚。

这些是内容:

  • 的pom.xml

    while True:
        sides = int(input("How many sides on the shape are there? "))
        if sides in result:
            print(sides[result])
            break  # done, exit the loop
         print("You should enter a number between 3 and 8")
    
  • Servlet初始化程序(DeevigeServletInitializer.java)

    <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/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com</groupId>
        <artifactId>deevigeweb</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>deevigeweb Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.1.6.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.1.6.RELEASE</version>
            </dependency>
    
           <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-webmvc</artifactId>
               <version>4.1.6.RELEASE</version>
           </dependency>
    
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>jstl</artifactId>
              <version>1.2</version>
          </dependency>
    
       </dependencies>
       <build>
          <finalName>deevigeweb</finalName>
       </build>
    </project>
    
  • Web MVC配置(DeevigeWebMVCConfig.java)

    package com.deevigeweb.config;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    public class DeevigeServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
           return new Class[] {DeevigeWebMVCConfig.class};
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
           return null;
        }
    
        @Override
        protected String[] getServletMappings() {
           return new String[] {"/"};
        }
    
    }
    
  • 控制器(SearchViewController.java)

    package com.deevigeweb.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;
    
    @EnableWebMvc
    @Configuration
    @ComponentScan({"com.deevigeweb"})
    public class DeevigeWebMVCConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
             registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
    
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/views");
        resolver.setSuffix(".jsp");
        return resolver;
    }
    
    }
    
  • JSP (searchview.jsp)

    package com.deevigeweb.controllers.webapp;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    
    public class SearchViewController {
        @RequestMapping(value="/",method=RequestMethod.GET)
        public ModelAndView doLanding(){
            return new ModelAndView("searchview");
        }
    }
    
  • 目录结构 project structure

2 个答案:

答案 0 :(得分:1)

也许添加尾部斜杠可以解决这个问题:

resolver.setPrefix("/WEB-INF/views/");

还在@Controller班级

上添加SearchViewController注释

答案 1 :(得分:1)

var myValue: Int = 1 if let test = myValue as? Int { print("IS INT") // hits here } else { print("NOT INT") } 上添加班级 var myValue: Float = 1.1 if let test = myValue as? Int { print("IS INT") } else { print("NOT INT") // hits here } 注释。你错过了。

@Controller

现在,网址localhost:8080/deevigeweb/将返回SearchViewController页。

项目中不需要@Controller public class SearchViewController { @RequestMapping(value="/",method=RequestMethod.GET) public ModelAndView doLanding(){ return new ModelAndView("searchview"); } } ,因为这是基于纯Java的配置。