Springmvc + hibernate is a simple example.

Source: Internet
Author: User

Springmvc + hibernate is a simple example.

I just entered a new company and saw that all the projects were in the form of webservice + springmvc. I used Struts all the time before. I think it is necessary to record it. Because I have recorded the annotation injection method before, but I have not been in touch with it for so long, and I forget it. Fortunately, I recorded it in my blog.

Environment: spring4.0.4.RELEASE, hibernate4.3.10Final. The packages are all downloaded on the official website.

Springmvc + hibernate made by eclipse + mysql adopts the annotation form.

 

OK. Let's start with the next step.

Eclipse first sets up a web project. If you use web3.0, you need to create web. xml (not created by default)

First, let's take a look at web. xml.

 
 
  
   
Springmvc
  
  
   
    
ContextConfigLocation
   
   
    
Classpath: spring-beans.xml
   
  
  
   
    
Org. springframework. web. context. ContextLoaderListener
   
  
  
  
   
    
EncodingFilter
   
   
    
Org. springframework. web. filter. CharacterEncodingFilter
   
   
    
     
Encoding
    
    
     
UTF-8
    
   
  
  
  
   
    
OpenSessionInViewFilter
   
   
    
Org. springframework. orm. hibernate4.support. OpenSessionInViewFilter
   
   
    
     
SessionFactoryBeanName
    
    
     
SessionFactory
    
   
  
  
   
    
OpenSessionInViewFilter
   
   
    
*. Do
   
  
  
  
   
    
Springmvc
   
   
    
Org. springframework. web. servlet. DispatcherServlet
   
   
    
1
   
  
  
   
    
Springmvc
   
   
    
*. Do
   
  
  
   
    
Index.html
   
   
    
Index.htm
   
   
    
Index. jsp
   
   
    
Default.html
   
   
    
Default.htm
   
   
    
Default. jsp
   
  
 

 

Springmvc the servlet name is associated with the configuration file under the WEB-INF, the naming method is [servletname]-servlet. xml, I am here springmvc.

Creating a WEB-INF under a springmvc-servlet.xml is as follows:

 
 
  
  
   
   
    
    
     
      
     
    
   
  
 

 

The view parser divides the prefix suffix and then merges it with the string returned by your controller to form a page path. (All are modifiable paths)

Put it under the WEB-INF just for safety.

Basically, springmvc needs to be configured.

The controller code is as follows:

/****@authorDaven.Tsang**/@ControllerpublicclassHelloController{@Resource(name=userService)privateUserServiceuserService;/****@paramname*@parammodel*@return*/@RequestMapping(value=/hello.do)publicStringsayHello(Modelmodel){Stringsql=fromUser;List
 
  users=userService.findAll(sql);model.addAttribute(users,users);returnhello;}}
 

 

UserService is injected into the business. My current code is divided into three layers, dao, service, controller (equivalent to the previous action) can also be said to be a view. Only the controller is used to control the view.

The returned value "hello" is as mentioned above. It defines the path of a page together with the prefix + hello + suffix.

See index. jsp and hello. jsp.

Index. jsp is as follows:

 

 

 

If your user class has these attributes, you can get the parameters directly in the controller. (Automatic encapsulation)

 

Hello. jsp

 

$ {S. user_name}

 

Download the project.

Basically, such a project is ready.

 

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.