Springmvc 请求转发 No mapping found for HTTP request

If you are using tags like @RequestMapping, @Controller, etc, you have to use 

<mvc:annotation-driven /> instead of <annotation-driven />.


MVC Simplifications in Spring 3.0

As Juergen and Arjen have mentioned, Java developers everywhere have a smooth upgrade with Spring 3.0. Now that Spring 3 is out, I'd like to take you through some of the new MVCfeatures you may not know about. I hope you find these features useful and can start putting them to work in your web applications immediately.

This is also the start of a series on "Spring 3 Simplifications", so expect more posts like these in the coming days and weeks.

Configuration Simplification

Spring 3 introduces a mvc namespace that greatly simplifies Spring MVC setup. Along with other enhancements, it has never been easier to get Spring web applications up and running. This can be illustrated by the mvc-basic sample, which I will now walk you through.

mvc-basic has been designed to illustrate a basic set of Spring MVC features. The project is available at our spring-samples SVN repository, is buildable with Maven, and is importable into Eclipse. Start your review with web.xml and notice the configuration there. Notably, a DispatcherServlet is configured with a single master Spring configuration file that initializes all other application components. The DispatcherServlet is configured as the default servlet for the application (mapped to "/"), allowing for clean, REST-ful URLs.

Inside the master servlet-context.xml, you'll find a typical setup. On the first line, component scanning has been turned on to discover application components from the classpath. On the next line, you'll find the first new Spring MVC 3 feature in action:

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

This tag registers the HandlerMapping and HandlerAdapter required to dispatch requests to your @Controllers. In addition, it applies sensible defaults based on what is present in your classpath. Such defaults include:

  • Using the Spring 3 Type ConversionService as a simpler and more robust alternative to JavaBeans PropertyEditors
  • Support for formatting Number fields with @NumberFormat
  • Support for formatting Date, Calendar, and Joda Time fields with @DateTimeFormat, if Joda Time is on the classpath
  • Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is on the classpath
  • Support for reading and writing XML, if JAXB is on the classpath
  • Support for reading and writing JSON, if Jackson is on the classpath

Pretty cool, huh?

Moving on, the next line demonstrates another new feature:

<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="welcome" />

Behind the scenes, mvc:view-controller registers a ParameterizableViewController that selects a view for rendering. In this case, when "/" is requested, the welcome view is rendered. The actual view template is a .jsp resolved inside the /WEB-INF/views directory.

Moving on, the next line shows yet another new feature:

<!-- Configures Handler Interceptors -->    
<mvc:interceptors>
    <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>

The mvc:interceptors tag allows you to register HandlerInterceptors to apply to all controllers. Previously, to do this you had to explicitly register such interceptors with eachHandlerMapping bean, which was repetitive. Also note this tag now lets you restrict which URL paths certain interceptors apply to.

Moving on, the next line hightlights a new feature added in version 3.0.4:

<!-- Handles GET requests for /resources/** by efficiently serving static content in the ${webappRoot}/resources dir -->
<mvc:resources mapping="/resources/**" location="/resources/" />

The mvc:resources tag lets you configure a handler for static resources, such as css and javascript files. In this case, requests for /resources/** are mapped to files inside the /resources directory.

Putting things in motion, if you deploy the sample you should see the welcome view render:

mvc-basic

Feel free to activate the different language links to have the LocaleChangeInterceptor switch the user locale.

Data Binding Simplification

The next set of new features I'll illustrate pertain to @Controller binding and validation. As Iblogged about a few weeks ago, there is a lot new in this area.

In the sample, if you activate the @Controller Example link the following form should render:

mvc-basic-form

From there, if you change the locale you should see internationalized field formatting kick in. For example, switching from en to de would cause the Renewal Date 12/21/10 to be formatted 21.12.10. This behavior and the form's validation rules are driven by model annotations:

public class Account {

    @NotNull
    @Size(min=1, max=25)
    private String name;

    @NotNull
    @NumberFormat(style=Style.CURRENCY)
    private BigDecimal balance = new BigDecimal("1000");

    @NotNull
    @NumberFormat(style=Style.PERCENT)
    private BigDecimal equityAllocation = new BigDecimal(".60");

    @DateTimeFormat(style="S-")
    @Future
    private Date renewalDate = new Date(new Date().getTime() + 31536000000L);

}

Form submit is handled by the following AccountController method:

@RequestMapping(method=RequestMethod.POST)
public String create(@Valid Account account, BindingResult result) {
    if (result.hasErrors()) {
        return "account/createForm";
    }
    this.accounts.put(account.assignId(), account);
    return "redirect:/account/" + account.getId();
}

This method is called after binding and validation, where validation of the Account input is triggered by the @Valid annotation. If there are any validation errors, the createForm will be re-rendered, else the Account will be saved and the user will be redirected; e.g. to http://localhost:8080/mvc-basic/account/1.

For an illustration of another cool new feature, try requesting an account that does not exist; e.g. /account/99.

Summary

Spring 3 is a great release containing numerous new features and simplifications across many exciting areas. I hope you found this post on some of the new Spring MVC enhancements useful. As I mentioned at the top, expect more to come in the "Spring 3 Simplifications" series, where we will continue to show new and interesting things you can do with the latest version of the framework.

Happy Holidays!


No mapping found for HTTP request with URI [/spmvctst/sigup.do] in DispatcherServlet with name 'springMVC'表示在DispatcherServlet中找不到对应的URI映射。这个错误常见于Spring MVC框架中。根据引用和引用的警告信息,这个错误可能是因为没有在DispatcherServlet的配置文件中正确配置URI映射的处理器(Handler)。具体来说,可能是没有在配置文件中添加对应的@Controller或@RequestMapping注解的类,或者没有在配置文件中配置<mvc:annotation-driven/>和<context:component-scan/>标签。引用中提到了这两个标签的作用,其中<context:component-scan/>用于扫描@Controller注解的类,而<mvc:annotation-driven/>用于启动Spring MVC的注解功能。 为解决这个问题,你可以按照以下步骤进行操作: 1. 确认你的DispatcherServlet配置文件中是否正确配置了@Controller注解的类。确保这些类被正确扫描到。可以使用<context:component-scan/>标签指定要扫描的包。 2. 确认你的DispatcherServlet配置文件中是否配置了<mvc:annotation-driven/>标签。这个标签用于启动Spring MVC的注解功能,确保HandlerMapping、HandlerAdapter和ExceptionResolver等处理器被正确注册。 3. 如果上述步骤都正确配置了,但仍然出现错误,请检查你的URI是否与配置文件中的URI映射匹配。确认是否存在拼写错误或者路径错误。 通过以上步骤,可以解决"No mapping found for HTTP request"的错误,确保DispatcherServlet能够找到正确的URI映射并处理请求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [No mapping found for HTTP request with URI [/.../...] in DispatcherServlet](https://blog.csdn.net/qq_45592174/article/details/112914031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [No mapping found for HTTP request with URI [/user/login.do] in DispatcherServlet with name 'dispatch](https://blog.csdn.net/Drrier/article/details/79892351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值