Early projects were relatively simple, mostly directly dealt with JSP, Servlet + JDBC. Later, Struts1(Struts2)+Spring+Hibernate was used to drive project development strictly according to the concept of layering. This time, Spring MVC was used to replace Struts for development.
MVC is already a very important part of modern web development. Let’s introduce the development environment construction of SpringMVC+Spring3+Hibernate4.
Let’s take a look at the project structure:
The specific code will not be demonstrated anymore, it mainly takes a very ordinary route, the structure of mvc-servcie-dao-hibernate, the source code can be downloaded, mainly look at the configuration file. See the comments for explanation
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SpringMVC</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Configure Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Configure SpringMVC --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Set character set--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Switch to control Session--> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Annotation scan package --> <context:component-scan base-package="com.jialin" /> <!-- Open annotation scheme 1 --> <!-- Annotation method processing-> <!-- <bean /> --> <!-- Annotation class mapping processing--> <!-- <bean></bean> --> <!-- Open annotation scheme 2 --> <mvc:annotation-driven /> <!-- Static resource access, solution 1 --> <mvc:resources location="/img/" mapping="/img/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!-- Static resource access, Scheme 2 --> <!-- <mvc:default-servlet-handler/> --> <!-- View interpretation class --> <bean id="viewResolver" > <property name="prefix" value="/"></property> <!-- Can be empty, convenient to implement the logic of the view interpretation class based on your extension --> <property name="suffix" value=".jsp"></property> </bean> <!-- Upload file bean --> <!-- <bean id="multipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean> --> </beans>
spring-hibernate.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Configure Data Source--> <bean id="dataSource" > <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1/springmvc" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <!-- Configure hibernate SessionFactory--> <bean id="sessionFactory" > <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </prop> </property> <property name="configLocations"> <list> <value> classpath*:config/hibernate/hibernate.cfg.xml </value> </list> </property> </bean> <!-- Transaction Manager--> <bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- Transaction Agent Class--> <bean id="transactionBese" lazy-init="true" abstract="true"> <property name="transactionManager" ref="transactionManager"></property> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="del*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="get*">PROPAGATION_NEVER</prop> </props> </property> </bean> </beans>
spring-core.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Introduce other configuration files, which can be multiple --> <import resource="classpath*:config/spring/spring-user.xml"/> </beans>
spring-user.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [ <!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml"> ]> <beans> <!-- Spring Bean --> <bean id="userDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userManagerBase"> <property name="userDao" ref="userDao"></property> </bean> <!-- parent is transactionBese, indicating that transactions are supported --> <bean id="userManager" parent="transactionBese"> <property name="target" ref="userManagerBase"></property> </bean> </beans>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Introducing classes that require mapping --> <mapping/> </session-factory> </hibernate-configuration>
Let's take a look at Controller
package com.jialin.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.jianin.entity.User; import com.jianin.service.IUserManager; @Controller //Struts-like Action @RequestMapping("/user") public class UserController { @Resource(name="userManager") // Get the id of the bean in the spring configuration file as userManager, and inject private IUserManager userManager; @RequestMapping("/addUser") // Request url address mapping, similar to Struts-like action-mapping public String addUser(User user){ if(userManager.addUser(user)) { // Redirect return "redirect:/user/getAllUser"; }else { return "/fail"; } } @RequestMapping("/updateUser") public String updateUser(User user,HttpServletRequest request){ if (userManager.updateUser(user)) { user = userManager.getOneUser(user); request.setAttribute("user", user); return "/UserEdit"; }else { return "/fail"; } } @RequestMapping("/delUser") public void delUser(User user,HttpServletResponse response){ String result = "{/"result/":/"error/"}"; if(userManager.delUser(user)){ result = "{/"result/":/"success/"}"; } PrintWriter out = null; response.setContentType("application/json"); try { out = response.getWriter(); out.write(result); } catch (IOException e) { e.printStackTrace(); } } @RequestMapping("/toAddUser") public String toAddUser(){ return "/UserAdd"; } @RequestMapping("/toUpdateUser") public String toUpdateUser(User user,HttpServletRequest request){ User user1=userManager.getOneUser(user); request.setAttribute("user1", user1); return "/UserEdit"; } @RequestMapping("/getAllUser") public String getAllUser(HttpServletRequest request){ List userList=userManager.getAllUser(); request.setAttribute("userlist", userList); return "/UserMain"; } }Source code download...
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.