Introduction to Spring MVC (1)--Non-annotated mode

Source: Internet
Author: User

Development tools: MyEclipse 8.5,jdk1.6,tomcat7.0


1, establish WebProject project, import Spring 3.2.3 related jar package

2, configure the front-end controller in Web. Dispatcherservlet.

<servlet><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <servlet-name>springmvc</servlet-name></servlet><servlet-mapping><servlet-name> Springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>

3, create the SPRINGMVC configuration file under the Web-inf folder, the file name is called "Servlet-name"-servlet.xml in Step 2, the configuration file name in the example is called " Springmvc-servlet.xml "need to replenish the header of the configuration file

<beans xmlns= "http:

4. Write the Handlermap bean configuration in the Springmvc configuration file, where the example is to find the controller based on Beanname.


<!--setup handlemapping find the corresponding controller--<bean id= "urlmapping" class= "according to Beanname Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping "> </bean>

5, create controller, and in the control, according to Modelandview set the returned page and parameters.

public class Hellocontroller extends abstractcontroller{@Overrideprotected modelandview handlerequestinternal ( HttpServletRequest request,httpservletresponse response) throws Exception {String username = request.getparameter (" Username ");//view, the parameter in the constructor is the returned page name Modelandview Mav = new Modelandview ();//another notation Modelandview Mav = new Modelandview (" Hell O "); Mav.setviewname (" Hello ");//Parameter Mav.addobject (" username ", username) to the page; return MAV;}

6, in the SPRINGMVC configuration file, configure the View parser bean, and return the page prefix, suffix.

<!--configuration View resolver Modelandview--<bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/"> </property> <property name= "suffix" value= ". jsp" ></property> </bean>

7. In the middle of SPRINGMVC configuration, configure the controller's bean.

<!--configuring Controllers--<bean id= "Hellocontroller" name= "/hellocontroller.do" class= "Com.controller.HelloController" > </bean>

Note: In the bean configuration of the controller, especially if the previous Handlermap setting is based on Beanname, the bean's Name property must be stated here.


8. The requested JSP page login.jsp, note the action request format, and match the Url-pattern in Web. xml

<body> <form action= "hellocontroller.do" type= "POST" > <input type= "text" name= "username"/> < Input type= "Submit" value= "Submission"/> </form></body>

9. Response Page hello.jsp

<body> Hello,${username}</body>

Steps above.


In the process of doing the example, an error has occurred

Error:no mapping found for HTTP request with URI [/springmvc01/hellocontroller.do] in Dispatcherservlet with Name ' SPRINGM vc

The reason for this error is that the controller's bean is not stated in the SPRINGMVC configuration, and the Handlermap is set according to the Beanname location controller.


Just the sauce.








This article is from the "Silent" blog, please be sure to keep this source http://bcptdtptp.blog.51cto.com/1475446/1723107

Introduction to Spring MVC (1)--Non-annotated mode

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.