SpringMVC framework project learning and analysis, springmvc framework project

Source: Internet
Author: User

SpringMVC framework project learning and analysis, springmvc framework project

I believe that programmers make mistakes not because they don't understand them, but because they think they understand everything.

Today, I want to organize SpringMVC, the framework used in the current project. I mentioned its advantages earlier and reduced the writing of configuration files, let's talk about the process of building your own framework (we recommend you learn Spring in action ):

Let's take a look at a flowchart:


First, configure DispatcherServlet in web. xml (the import package is not described in the previous project ):

<! -- Prepare the spring distributor servlet -->

<Servlet>

<Servlet-name> action </servlet-name>

<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>

</Servlet>

<Servlet-mapping>

<Servlet-name> action </servlet-name>

<Url-pattern> *. action </url-pattern>

</Servlet-mapping>

Create a controller HomeController:

Public class HomeController extendsimplements actcontroller {

ProtectedModelAndViewhandleRequestInternal (HttpServletRequest arg0,

HttpServletResponse arg1) throws Exception {

System. out. println ("hello world ");

Returnnew ModelAndView ("index ");

}

}

Configure the WEB-INF file under the action-servlet.xml:

<! -- Controller -->

<Bean name = "/home. action" class = "cn. itcast. springmvc. controller. HomeController">

</Bean>

<! -- Internal resource view parser -->

<Bean id = "internalResourceViewResolver" class = "org. springframework. web. servlet. view. InternalResourceViewResolver">

<! -- Prefix -->

<Property name = "prefix" value = "/WEB-INF/jsps/"/>

<! -- Suffix -->

<Property name = "suffix" value = ". jsp"/>

</Bean>

When talking about processor ing, let's talk about the following: (bean name url processor ing, simple url processor ing, controller class name processor ing)

<! -- Bean name url processor ing -->

<Beanid = "beanNameUrlHandlerMapping" class = "org. springframework. web. servlet. handler. BeanNameUrlHandlerMapping">

<Property name = "order" value = "1"/>

</Bean>

<! -- Simple url processor ing -->

<Beanid = "simpleUrlHandlerMapping" class = "org. springframework. web. servlet. handler. SimpleUrlHandlerMapping">

<Propertyname = "mappings">

<Props>

<Prop key = "/bbb. action"> helloController </prop>

</Props>

</Property>

<Propertyname = "order" value = "2"/>

</Bean>

<! -- Controller class name processor ing -->

<Beanid = "controllerClassNameHandlerMapping" class = "org. springframework. web. servlet. mvc. support. ControllerClassNameHandlerMapping">

<Property name = "order" value = "3"/>

</Bean>

There are several Controller types:

• CommandController)

* Inherit the AbstractCommandController class and override the handle method.

* Register the command class and command name through the constructor, for example:

PublicMyCommandController (){

// Register the command class

This. SetCommandClass (Person.Class);

// Command name

This. SetCommandName ("person ");

}

• FormController (Form Controller)

* The SimpleFormController class must be inherited and the doSubmitAction method must be rewritten.

* Register the command class and command name through the constructor, for example:

PublicMyFormController (){

This. SetCommandClass (Person.Class);

This. SetCommandName ("person ");

}

* Configure the form controller in the spring configuration file, for example:

<! -- Form Controller -->

<Beanid = "myFormController" name = "/form. action" class = "cn. itcast. controller. MyFormController">

<Propertyname = "successView" value = "success"/>

<Propertyname = "formView" value = "personForm"/>

</Bean>

• WizardFormController (wizard form Controller)

* You must inherit the AbstractWizardFormController class and override the processFinish method.

* Register the command class and command name through the constructor, for example:

PublicMyWizardFormController (){

This. SetCommandClass (Person.Class);

This. SetCommandName ("person ");

}

* Configure the wizard form controller in the spring configuration file, for example:

<! -- Wizard form Controller -->

<Bean name = "/wizard. action" id = "myWizardFormControlle" class = "cn. itcast. controller. MyWizardFormController">

<Propertyname = "pages">

<List>

<Value> wizard/1 </value>

<Value> wizard/2 </value>

<Value> wizard/3 </value>

</List>

</Property>

</Bean>

The rest is the spring configuration, so we use the annotation method to annotate dao, service, and controller layers respectively:

Dao: @ Repository service: @ Service controller: @ Controller

If we do not configure the method jump according to the struts. xml method, then how can we achieve the page and method jump? springMVC uses the method body to bind the foreground method body to the background through annotations:

@ RequestMap (value = "") Fill in the method path and method name in value. You can also set the method methed for method acceptance, that is, the get and post methods of the form, at the same time, we also handed over all views to the Controller, that is, the front-end can directly jump, we also had to perform a background processing, one is to load data through the background, second, it facilitates permission control and controls all user operations on the server. This is an advantage of coarse-grained permission control. When the jump starts, how to load the view after the jump is that there must be a new url generated. Here there is a new class. The ModelAndView class is used to store the url after the jump, and the data to be loaded. After practice, I would like to say that you must pay attention to the ModelAndView class packaging problem. We use Spring. the servlet package is not clear to me, but it is true that I have entered this misunderstanding many times, and this bug is hard to find. So I hope the future will be careful, careful, and considerate.

Another point I think needs to be noted down is that when we used the struts + hibernate + spring framework, we usually perform pseudo-static processing on the front-end page. The advantage of doing so is to speed up the website, to facilitate the optimization of Search Engine calls, the website stability is improved. Previously, the ssh framework used the urlrewrite framework for pseudo-static processing. Now we don't need this technology. After springMVC runs, it takes time to compile for the first time, and the rest will generate static processing. In addition, our websites often like to hide their own technologies. We often see that most of the websites end with html. We can use the Framework to do this and use html instead of action-like actions, every action ends with html.

Finally, the reuse of each framework is a complete analysis of the project and the strength behind the open source. The best way for developers to familiarize themselves with the framework is to learn while using it and continuously coding, from debugging, we will learn the joy and sorrow brought out by the Framework. Well, that's it today.

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.