Spring MVC configuration + dbcp data source +jdbctemplate

Source: Internet
Author: User
Tags aop exception handling

Summary:

Put the spring jar package into the Lib directory, the Jar can be based on the function you want to choose, if lazy or do not want to use the function later put together, pay attention to not put the document jar and source jar

Suddenly think of spring's requirements for the environment, also forget to say that my JDK version is 1.6, the database will use MySQL, Application server is Tomcat7.0

First go to spring official website to download the full spring package, including Libs, Docs and schema,spring version is 3.2.4

Let's take a look at the contents of the Spring Lib package:


The two jars in the above picture except the red box are all spring's official jar packages, the jar in the red box we'll use when we configure the transaction, we'll talk about it later. Let's take a closer look at the jar packages provided by spring to see that each module corresponds to 3 jar packages, Sources package (source code), Javadoc package (document) and compiled jar.

And then we look at what the modules are, let's take a look at the overview diagram provided in the spring document to see if the jars are all corresponding to their modules.


The first module, data storage/integration, it also includes jdbc,orm (object-relational mapping), OXM (Object/xml mapping), JMS (javamessaging service), Transactions (business)

The second module, the Web (mvc/remoting), which also contains the Web layer, web-servlet (containing the implementation of Spring MVC), web-portlet,web-struts

The third module, AOP (aspect-orented programming) is what we usually call aspect-oriented programming

Fourth module, aspects, provides integration with ASPECTJ

Fifth module, instrumentation, provides support for class meters and class loader implementations

The sixth module, Corecontainer (Core container), which also contains beans and core (which provides the basic parts of the framework, including control inversion and dependency injection characteristics), context,expression Language ( Provides a powerful expression language for querying and manipulating object graphs at run time

The seventh module, test, provides the functionality to test the spring component, which is said to be quite powerful, haha

Classic Introduction:

SOURCE structure


Look at the jar and it should all correspond. We continue,

Create a new Web project in MyEclipse, creating the relevant directory structure, such as:


The Config folder is a sources folders used to place configuration files.

Put the spring jar package into the Lib directory, the Jar can be based on the function you want to choose, if lazy or do not want to use the function later put together, pay attention to not put the document jar and source jar

Suddenly think of spring's requirements for the environment, also forget to say that my JDK version is 1.6, the database will use MySQL, Application server is Tomcat7.0

OK, now for the SPRINGMVC configuration, we all know that the spring configuration file is called Applicationcontext.xml and SPRINGMVC configuration file will be called Springmvc.xml actually these two files written in one can, We'll just name Springmvc.xml.

In the config directory to create a springmvc.xml file, we first configure the Sprigmvc.xml file,

Configure the Spring file header

XML code

Xmlns= "Http://www.springframework.org/schema/beans"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xmlns:p= "http://www.springframework.org/schema/p"

xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"

xmlns:context= "Http://www.springframework.org/schema/context"

xmlns:tx= "Http://www.springframework.org/schema/tx"

Xmlns:mvc= "Http://www.springframework.org/schema/mvc"

Xsi:schemalocation= "Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

Http://www.springframework.org/schema/aop

Http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-3.2.xsd

Http://www.springframework.org/schema/tx

Http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

Http://www.springframework.org/schema/mvc

Http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "default-lazy-init=" true ">

File header is mainly for sping. xsd file references, personal views, not guaranteed accurate, you can click to connect to see, for example: http://www.springframework.org/schema/mvc/

You can see the various versions of spring-mvc*.xsd files provided by spring


And then you remember, with what you cite, I quote, have AOP,CONTEXT,TX,MVC ....

Next Add annotation support:

XML code

Automatic scan of spring components, can be configured to the engineering root directory, such as com.xg.myspring, if you want to learn more please see more detailed code

XML code

Configure the view resolution, that is, the background to the page jump

When the default page jumps, the path will find the *.jsp file from the page/directory

XML code

Then make an exception handling configuration:

When an exception occurs, the program jumps to the specified error page, enhancing the program's friendliness.

This makes a common configuration, because exception is the parent of the exception and jumps to the error.jsp file under the error directory whenever an exception occurs

Java code

Error/error

Next configure the database, we use DBCP type data source, add MySQL database connection Jar,common-dbcp.jar in the Lib directory, remember to add Common-logging.jar Also, spring log is used by default.

Put the database configuration file into the Config directory.

The contents of the Jdbc.properties file are as follows:

Java code

Jdbc.driverclassname=com.mysql.jdbc.driver

Jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/report

Jdbc.username=root

jdbc.password=12345

Here's the code for configuring the data source:

XML code

In Configuring a JdbcTemplate

Java code

Springmvc.xml configure so much for the time being, then configure the Web. xml file

Configure spring to listen to the context first

Java code

Org.springframework.web.context.ContextLoaderListener

Configure the path to the Contextconfiglocation,spirngmvc.xml

Java code

Contextconfiglocation

Classpath:springmvc.xml

Configuring the Spring Distributor

Java code

Springmvc

Org.springframework.web.servlet.DispatcherServlet

Contextconfiglocation

Classpath:springmvc.xml

1

Look carefully will find configured two times contextconfiglocation, here http://minglisoft.cn/technology have explanation.

Now that the configuration is basically complete, then add the test code, we will follow the MVC pattern to add a control a service a DAO, add a landing page, a prompt to log on to the successful main page

Controller Systemusercontrol.java

Java code

Package Com.xg.myspring.control;

Import Java.io.File;

Import Java.io.FileInputStream;

Import java.io.IOException;

Import java.util.List;

Import java.util.Properties;

Import Javax.annotation.Resource;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

Import Org.springframework.stereotype.Controller;

Import Org.springframework.ui.Model;

Import org.springframework.web.bind.annotation.PathVariable;

Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.RequestMethod;

Import Org.springframework.web.servlet.ModelAndView;

Import Org.springframework.web.servlet.View;

Import Com.xg.myspring.entity.SystemUser;

Import Com.xg.myspring.service.SystemUserService;

@Controller

@RequestMapping ("/systemuser")

public class Systemusercontrol {

@Resource

Private Systemuserservice Systemuserservice;

@RequestMapping (value = "/login", method = Requestmethod.get)

Public Modelandview Login (systemuser user) {

String message = "Login succeeded!";

Modelandview mv = new Modelandview ("index");

Mv.addobject ("user", user);

Mv.addobject ("message", message);

return MV;

}

@RequestMapping (value = "/login", method = Requestmethod.post)

Public String login2 (httpservletrequest request, httpservletresponse response, systemuser user) {

Request.getsession (). SetAttribute ("user", user);

Request.getsession (). SetAttribute ("message", "Login successful!");

return "redirect:/page/index.jsp";

}

@RequestMapping ("/querylist")

Public String querylist (HttpServletRequest request) {

List List = null;

String sql = "SELECT * from systemuserinfotable";

List = systemuserservice.queryuserlist (SQL);

Request.setattribute ("list", list);

Return "index";

}

}

The Systemuserserviceimpl.java interface is omitted.

Java code

Package Com.xg.myspring.service.impl;

Import java.util.List;

Import Javax.annotation.Resource;

Import Org.springframework.stereotype.Service;

Import Com.xg.myspring.dao.SystemUserDao;

Import Com.xg.myspring.entity.SystemUser;

Import Com.xg.myspring.service.SystemUserService;

@Service

public class Systemuserserviceimpl implements Systemuserservice {

@Resource

Private Systemuserdao Systemuserdao;

public void Addsystemuser (SystemUser systemuser) {

Systemuserdao.addsystemuser (systemuser);

}

public void Deletesystemuser (String sql) {

Systemuserdao.deletesystemuser (SQL);

}

Public systemuser Getsystemuserbyid (String sql) {

return Systemuserdao.getsystemuserbyid (SQL);

}

Public List queryuserlist (String sql) {

Systemuserdao.addsystemuser (New SystemUser ());

return systemuserdao.queryuserlist (SQL);

}

}

Systemuserdaoimpl.java, the interface is omitted.

Java code

Package Com.xg.myspring.dao.impl;

Import Java.util.Date;

Import java.util.List;

Import Javax.annotation.Resource;

Import Org.springframework.jdbc.core.BeanPropertyRowMapper;

Import Org.springframework.jdbc.core.JdbcTemplate;

Import Org.springframework.stereotype.Repository;

Import Com.xg.myspring.dao.SystemUserDao;

Import Com.xg.myspring.entity.SystemUser;

@Repository ("Systemuserdao")

public class Systemuserdaoimpl implements Systemuserdao {

@Resource

Private JdbcTemplate JdbcTemplate;

public void Addsystemuser (SystemUser systemuser) {

Date Date=new date ();

String sql= "INSERT into systemuserinfotable values (' 000_" +date.gettime () + "', ' abc ' +date.gettime () +" ', ' abc ', ' 1 ', ' 1 ', ' Test ') ";

if (systemuser!=null) {

Jdbctemplate.execute (SQL);

}else{

throw new NullPointerException ();

}

}

public void Deletesystemuser (String sql) {

}

Public List queryuserlist (String sql) {

List List = Jdbctemplate.query (sql, New Beanpropertyrowmapper (Systemuser.class));

return list;

}

Public systemuser Getsystemuserbyid (String sql) {

return jdbctemplate.queryforobject (SQL, New Beanpropertyrowmapper (Systemuser.class));

}

}

Now run, report Java.lang.ClassNotFoundException:org.apache.commons.pool.KeyedObjectPoolFactory exception, the lack of Common-pool.jar

Together to add Jstl.jar and Servlet-api.jar, run it is no problem

Landing


Main Page


Welcome to study the relevant technology to learn about the framework of technology or the source of friends directly add to seek (Penguin): 2042849237

More detailed source code reference: Http://minglisoft.cn/technology

Spring MVC configuration + dbcp data source +jdbctemplate

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.