Spring+spring mvc+hibernate Environment collocation

Source: Internet
Author: User

Spring+spring mvc+hibernate Environment collocation

Spring+spring Mvc+hibernate referred to as "SSH". The spring container is the core of spring, which is responsible for managing Java components in spring. Spring's core mechanism: Dependency Injection. Hibernate is a good ORM (Relational object mapping) framework. Spring+spring mvc+hibernate Environment collocation steps:


1. Build spring+hibernate environment (consistent with SSH build steps)

(1) Add spring+hibernate to the rack package.

2, build SPRINGMVC environment
(1) Add Springmvc Dependency Package: Spring-webmvc.jar
(2) Configuring Web. Xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" XM Lns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http:/ /java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <!--Spring Listener:--<listener> <listener-clas S>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- Specify the spring file path-<context-param> <param-name>contextConfigLocation</param-name> <param-va Lue>classpath:applicationcontext.xml</param-value> </context-param> <!--===================== Configure Spring MVC start=================================-<servlet> <servlet-name>springmvc</servle T-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--with Place Springmvc Road--<Init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath: Springmvc.xml</param-value> </init-param> <!-->=0 load servlet on behalf of the Web container (number is priority)---& lt;load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>                  Springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web- App>

(3) Configuration Springmvc.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:tx= "Http://www.springframework.org/schema/tx" 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-2.5.xsd http://www.springframework.org /schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.spring Framework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <!--SPRINGMVC The configuration rules are the same as spring: seamless Integration--<!--configuration View--<bean id= "Internalview" class= "Org.springframewor  K.web.servlet.view.internalresourceviewresolver "> <!--Configuration View prefixes--<!--<property   Name= "prefix" value= "web-inf/jsp/"/>-->     <property name= "prefix" value= "/"/> <!--configuration view suffix--<property name= "suffix" value = ". jsp"/> </bean> <!--configuration URL map--<bean id= "urlmpping" class= "Org.springframewor                K.web.servlet.handler.simpleurlhandlermapping "> <property name=" Mappings "> <props> <prop key= "usercontroller.do" >userController</prop> <prop key= "Perpocontroller.do" & gt;perpocontroller</prop> <prop key= "Multimethodcontroller.do" &GT;MULTIMETHODCONTROLLER&LT;/PROP&G            T        </props> </property> </bean> <!--multi-method call rule--<bean id= "Parametermethod" class= "Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" > <!--value free; Yes or a meaning--<property name= "paramname" value= "method"/> </bean> <!--the controller to the spring Manager--&Gt <bean id= "Usercontroller" class= "Com.ssh.SpringMVC.controller.UserController" ></bean> <bean id= "    Perpocontroller "class=" Com.ssh.SpringMVC.controller.PerpoController "></bean> <!-- Configure multiple methods in one Controllet--<bean id= "Multimethodcontroller" class= " Com.ssh.SpringMVC.controller.MultiMenthodController "> <!--Injection Multi-method call rule--<property name=" M Ethodnameresolver "ref=" Parametermethod "></property> </bean></beans>

Log In page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Form submission Processing Multimenthodcontroller.java file

Package Com.msit.springmvc.controller;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.modelandview;import Org.springframework.web.servlet.mvc.multiaction.multiactioncontroller;public class MultiMenthodController extends Multiactioncontroller {            /**     * url:http://localhost:8080/springmvcday1/multimethodcontroller.do?method= Login     * @return     *    /Public Modelandview login (httpservletrequest request,httpservletresponse response) {        System.out.println ("Here is the login method" +request.getparameter ("username"));        return new Modelandview ("myjsp");    }        Public Modelandview Update (httpservletrequest request,httpservletresponse response) {        System.out.println (" Here is the Update method ");        return new Modelandview ("myjsp");}    }

Spring+spring mvc+hibernate Environment collocation

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.