7

我已经搜索了谷歌、stackoverflow 和我可以查看的每个论坛几天,我的键盘有成为头撞目标的可怕风险。

我正在运行一个非常小的带有无 XML 设置的 Spring 3.1 MVC。问题是当我启动它时,我看到了;

INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/start.action],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.start(javax.servlet.http.HttpServletRequest)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/*],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView com.xxxxxx.info.HomeController.home(java.util.Locale,org.springframework.ui.Model)

然而,当我尝试访问其中任何一个 URL 时,我看到我的控制器内的日志记录语句触发,然后立即获取;

INFO : com.xxxxxx.info.HomeController - Welcome home! the client locale is en_US
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WEB-INF/jsps/home.jsp] in DispatcherServlet with name 'dispatcher'

这是我的源文件。

初始化器 -

public class Initializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
        mvcContext.register(MvcConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }
}

MvcConfig -

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.xxxxxx.info")    
public class MvcConfig {
    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsps/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

控制器 -

@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /** Simply selects the home view to render by returning its name. */
    @RequestMapping(value = "/*")
    public ModelAndView home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is " + locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate);
        return new ModelAndView( "home", model.asMap() );
    }

    @RequestMapping( value = "/start.action")
    public ModelAndView start(HttpServletRequest request) {
        logger.info("Starting!");
        return new ModelAndView( "start", null );
    }
}

将调度程序映射更改为“/”我发现建议的许多帖子似乎完全破坏了它。如果我用“/”重新启动服务器,那么 Spring 不会报告任何内容,也不会映射任何内容,只有一个 tomcat 启动日志,其他任何操作都没有。

我的文件结构是 -

| com.xxxxxx.info
    - Initializer.java
    - MvcConfig.java
    - HomeController.java
| src
    | main
        | webapp
            | WEB-INF
                | jsps
                    - home.jsp
                    - start.jsp

所以它似乎正确地击中了我的控制器,但是当它获得视图名称时,它没有解析到正确的位置。我在这里错过了什么,这似乎是我忽略的一些简单的事情......

4

2 回答 2

2

我猜这个jsp是由DispatcherServlet渲染的,它在类似的问题中讨论过

您应该考虑HomeControllerWebApplicationInitializer.

于 2012-11-14T13:06:57.813 回答
2

您将\*同时发送给DispatcherServlet您和您的HomeController

dispatcher.addMapping("/*");

@RequestMapping(value = "/*")
    public ModelAndView home(Locale locale, Model model)

我认为这是造成问题的原因。将您的控制器映射更改为类似

@RequestMapping(value = "/home")
    public ModelAndView home(Locale locale, Model model)

希望能帮助到你。

于 2012-11-14T13:20:03.383 回答