使用Spring MVC中的@ Controller,InternalResourceViewResolver和ModelAndView进行配置不起作用

时间:2017-08-05 21:24:50

标签: spring-mvc request-mapping

我是Spring MVC的新手。我知道有很多类似的问题,但我已经尝试了大部分问题几个小时,但仍然面临当我从浏览器请求 http://localhost:8080/SpringRESTFulExample/ 时出现404错误的问题。但是在控制台中没有任何异常。 这是我的代码,

RootConfig.java

@Configuration
@ComponentScan(basePackages = "springrestful_example")
public class RootConfig {

}

WebConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "springrestful_example")
public class WebConfig extends WebMvcConfigurerAdapter{

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

    .
    .
    .
}

WebInitializer.java

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
}

UserController.java

@RestController
public class UserController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView modelandView = new ModelAndView("index");
        return modelandView;
    }
    .
    .
}

index.jsp位于 /WEB-INF/views/index.jsp

enter image description here

依赖关系我使用

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.3.9.RELEASE</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.0</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.10.RELEASE</version>
</dependency>


<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
 </dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

这就是我得到的全部。如果我的代码错了,请纠正我。如果我看到在index.jsp正文中写的Hello World文本,我会很高兴。

1 个答案:

答案 0 :(得分:0)

您的Config课程包含哪些包?它们是否在您指定的basePackages中?尽量不要在组件扫描注释中指定basePackages。