Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

java - Spring MVC Mapping problem

I have what I thought was a simple Spring MVC app. However, I can seem to set the requestMappings correctly. What's strange is that the logs show that the url is mapped to the proper controller, yet the Dispatcher cant seem to find it at runtime. Any suggestions would be much appreciated:

Log

INFO: Mapped URL path [/app/index] onto handler     [com.noisyair.whatisayis.web.MainController@420a52f]
Jan 11, 2010 2:14:21 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/app/index.*] onto handler [com.noisyair.whatisayis.web.MainController@420a52f]
Jan 11, 2010 2:14:21 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/app/index/] onto handler [com.noisyair.whatisayis.web.MainController@420a52f]
Jan 11, 2010 2:14:21 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/app/tags/{tag}] onto handler [com.noisyair.whatisayis.web.SearchByTagController@7b3cb2c6]
Jan 11, 2010 2:14:21 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/app/tags/{tag}.*] onto handler [com.noisyair.whatisayis.web.SearchByTagController@7b3cb2c6]
Jan 11, 2010 2:14:21 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/app/tags/{tag}/] onto handler [com.noisyair.whatisayis.web.SearchByTagController@7b3cb2c6]
Jan 11, 2010 2:14:21 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'wisi': initialization completed in 237 ms
Jan 11, 2010 2:14:21 PM org.apache.catalina.core.StandardContext start
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/] has already been started
Jan 11, 2010 2:14:41 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/app/index] in DispatcherServlet with name 'wisi'

Web.xml file

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- The Spring MVC framework handles all of this stuff.  Just pass it along -->
<servlet>
    <servlet-name>wisi</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>wisi</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

Controller class:

@Controller
public class MainController {

@Autowired
private LearningEntryService learningEntryService;

public LearningEntryService getLearningEntryService() {
    return learningEntryService;
}

public void setLearningEntryService(LearningEntryService learningEntryService) {
    this.learningEntryService = learningEntryService;
}

@RequestMapping(value = "/app/index", method = RequestMethod.GET)
public ModelAndView sayHello(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    Map<String, Object> model = new HashMap<String, Object>();
    List<LearningEntry> le = learningEntryService.getLearningEntries();
    model.put("learningEntries", le);
    return new ModelAndView("main", model);
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You shouldn't duplicate "/app" in both @RequestMapping and <url-pattern>. That is, your sayHello now is mapped to "/app/app/index". You can write

@RequestMapping(value = "/index", method = RequestMethod.GET)

(Or you can declare DefaultAnnotationHandlerMapping bean in your config and set its allwaysUseFullPath property to true to override default behavior)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...