java – 无XML的Spring 3.1没有找到HTTP请求的映射

我已经搜索了谷歌,stackoverflow,以及我可以看几天的每个论坛,我的键盘很可能成为头部的目标.

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

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 "{[/*],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

因此它似乎正确地击中了我的控制器,但是当它获得视图名称时它没有解析到正确的位置.我在这里想念的是什么,这似乎是我忽略的简单……

解决方法

我猜这个jsp是由dispatcherServlet呈现的,它在 similar question中讨论过

您应该考虑在HomeController和WebApplicationInitializer中使用更具体的映射.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...