Spring mvc-Handler mappings (Handler Mapping)-Controller class name Handler mappings (Controllers class name Handler Mapping) Example (reprint practice)

Source: Internet
Author: User
Tags tomcat server tutorialspoint

The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_controllerclassnamehandlermapping.htm

Description: The sample is based on spring MVC 4.1.6.

The following example shows how to use the Spring WEB MVC framework to use the controller class name handler mappings. The Controllerclassnamehandlermapping class is a contract-based handler mapping class that maps URL requests to the names of the controllers mentioned in the configuration. The class uses the controller name and converts them to lowercase with "/". For example, Hellocontroller maps to the "/hello*" URL.

<Beans>   <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">      < Propertyname= "prefix"value= "/web-inf/jsp/"/>      < Propertyname= "suffix"value= ". jsp"/>   </Bean>   <Beanclass= "Org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>    <Beanclass= "Com.tutorialspoint.HelloController" />   <Beanclass= "Com.tutorialspoint.WelcomeController"/>   </Beans>

For example, using the above configuration, if it is a URI

    1. /helloworld.htm or/hello{any Letter}.htm,dispatcherservlet forwarded the request to Hellocontroller.

    2. /welcome.htm was requested, Dispatcherservlet forwarded the request to Welcomecontroller.

    3. /welcome.htm is requested, where W is uppercase, Dispatcherservlet will not find any controller and the server will throw a 404 status error.

First, let's use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web framework:

Steps Description
1 Create a name for the project TestWeb package under Com.tutorialspoint as explained in the Spring Mvc-hello World Example Chapter.
2 Create a Java class Hellocontroller,welcomecontroller under the Com.tutorialspoint package.
3 Create a view file under the JSP subfolder hello.jsp,welcome.jsp.
4 The final step is to create the contents of all the source and configuration files and export the application as described below.

Hellocontroller.java

 PackageCom.tutorialspoint;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController; Public classHellocontrollerextendsabstractcontroller{@OverrideprotectedModelandview handlerequestinternal (httpservletrequest request, httpservletresponse response)throwsException {Modelandview model=NewModelandview ("Hello"); Model.addobject ("Message", "Hello world!"); returnmodel; }}

Welcomecontroller.java

 PackageCom.tutorialspoint;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController; Public classWelcomecontrollerextendsabstractcontroller{@OverrideprotectedModelandview handlerequestinternal (httpservletrequest request, httpservletresponse response)throwsException {Modelandview model=NewModelandview ("Welcome"); Model.addobject ("Message", "welcome!"); returnmodel; }}

Testweb-servlet.xml

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd ">   <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">      < Propertyname= "prefix"value= "/web-inf/jsp/"/>      < Propertyname= "suffix"value= ". jsp"/>   </Bean>   <Beanclass= "Org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>    <Beanclass= "Com.tutorialspoint.HelloController" />   <Beanclass= "Com.tutorialspoint.WelcomeController"/>  </Beans>

hello.jsp

<%@ Page ContentType="text/html; Charset=utf-8" %><HTML><Head><title>Hello World</title></Head><Body>   <H2>${message}</H2></Body></HTML>

welcome.jsp

<%@ Page ContentType="text/html; Charset=utf-8" %><HTML><Head><title>Welcome</title></Head><Body>   <H2>${message}</H2></Body></HTML>

After you finish creating the source files and profiles, export the application. Right-click the application and use the export->war file option and save your testweb.war file in the Tomcat WebApps folder.

Now start your Tomcat server and make sure you can access other pages from the WebApps folder using a standard browser. Now try URL http://localhost:8080/TestWeb/helloWorld.htm, if everything in your spring Web application is fine, you should see the following results:

Try URL http://localhost:8080/TestWeb/hello.htm, if everything in the spring Web application is fine, you should see the following results:

Try URL http://localhost:8080/TestWeb/welcome.htm, if everything in your spring Web application is fine, you should see the following results:

Try URL http://localhost:8080/TestWeb/Welcome.htm, if everything in the spring Web application is fine, you should see the following results:

Maven Example:

https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test17

Spring mvc-Handler mappings (Handler Mapping)-Controller class name Handler mappings (Controllers class name Handler Mapping) Example (reprint practice)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.