Spring+springmvc
MVC, now seems to be getting popular using the SPRINGMVC framework, I myself use the feeling that is very good, indeed very comfortable, configuration is a little trouble at first, but the subsequent development is really refreshing!
SPRINGMVC configuration file
Contents: resource/config/spring, file name: Spring-mvc.xml
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 Xmlns:context= "Http://www.springframework.org/schema/context"5 Xmlns:mvc= "Http://www.springframework.org/schema/mvc"6 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.xsd ">7 8 <!--Automatic Scan Controller -9 <Context:component-scanBase-package= "Com.magic.rent.controller"/>Ten <!--View Rendering - One <BeanID= "Internalresourceviewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> A < Propertyname= "prefix"value= "/web-inf/views/"/> - < Propertyname= "suffix"value= ". jsp"/> - </Bean> the <!--Controller Mapper and controller adapter - - <Mvc:annotation-driven/> - </Beans>
Spring-mvc.xml
Spring configuration file
Contents: resource/config/spring, file name: Applicationcontext-service.xml
Well, this should belong to the spring configuration file, pop, forget before, just fill it up here together. This content is temporarily less, is the SERVICEC layer, later if there is any content, you can continue to fill in here, such as adding what tools or frameworks.
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context.xsd "> <!--Scan Service - <Context:component-scanBase-package= "Com.magic.rent.service"/> <!--Register Unified Exception Control - <BeanID= "Exception"class= "Com.magic.rent.exception.exhandler.CustomExceptionHandler"/></Beans>
Contents: resource/config/spring, file name: Applicationcontext-transaction.xml
This is primarily used for transactions. The AOP configuration is used, and other AOP configurations can refer to this format, but the AOP configuration is a bit of a hassle, or you need to take a good look at spring's document reconfiguration.
<?xml version= "1.0" encoding= "UTF-8"?>
<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd HTTP://WWW.SP RINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd "> <BeanID= "Datasourcetransactionmanager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> < Propertyname= "DataSource"ref= "DataSource"/> </Bean> <Tx:adviceID= "Txadvice"Transaction-manager= "Datasourcetransactionmanager"> <tx:attributes>
<--This part, is mainly based on the method name to match the qualification, but we are automatically generated by the MyBatis mapper interface, so in this side you have to follow the MyBatis naming specification to set the value of name. <Tx:methodname= "select*"Propagation= "REQUIRED"/> <Tx:methodname= "update*"Propagation= "REQUIRED"/> <Tx:methodname= "delete*"Propagation= "REQUIRED"/> <Tx:methodname= "insert*"Propagation= "REQUIRED"/>
</tx:attributes> </Tx:advice> <Aop:config> <Aop:advisorAdvice-ref= "Txadvice"pointcut= "Execution (* com.magic.rent.service.*.* (..))"/> </Aop:config></Beans>
Here, to do a small summary, in fact, this is a spring+springmvc+mybatis integration, this online has a lot of this similar example, this will not be too difficult, so far, is basically a simple configuration, configuration file is actually not much complex.
Spring Unified Exception Handling
The spring framework comes with the ability to unify exception handling in three ways, but I only introduce one, because this is the most common use of production.
Create a Directory
━java
┗exception (This class is similar to the service, controller, because the custom in this class can define a very detailed exception condition.) )
┣custom (Store custom exceptions for the actual business layer to throw)
┗exhandler (Controller storing exception handling)
━webapp
┣web-inf
┣admin (Administration page)
┣error (store error page)
┗views (store normal page)
To create an exception class control class
contents: Com.magic.rent.exception.exhandler, File name: Customexceptionhandler.java
PackageCom.magic.rent.exception.exhandler;Importcom.magic.rent.exception.custom.BusinessException;Importcom.magic.rent.exception.custom.ParameterException;ImportOrg.springframework.stereotype.Service;ImportOrg.springframework.web.servlet.HandlerExceptionResolver;ImportOrg.springframework.web.servlet.ModelAndView;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJava.util.HashMap;ImportJava.util.Map; Public classCustomExceptionHandlerImplementsHandlerexceptionresolver { PublicModelandview resolveexception (httpservletrequest httpservletrequest, HttpServletResponse HttpServletResponse, Object O, Exception ex) {Map<string, object> model =NewHashmap<string, object>(); Model.put ("Ex", ex); //turn to different pages based on different errors if(exinstanceofbusinessexception) { return NewModelandview (".. /error/business_error ", model); } Else if(exinstanceofparameterexception) { return NewModelandview (".. /error/parameter_error ", model); } Else { return NewModelandview (".. /error/404 ", model); } }}
The address of the Modelandview in the class is ". /error/xxx ", this is because we SPRINGMVC configuration" Spring-mvc.xml "in the internalresourceviewresolver tag set the prefix and suffix, the default prefix is to start from the Views folder to access the page, To change to the error folder, you have to write like this, of course, this class can be unified to optimize again, such as ". /error/"Extract to unify settings. Of course, this way can also be used to return to JSON, instead of having to jump to the interface, the JSON may be more in line with the requirements, after all, is now mostly using AJAX to do the interaction. If you change it later, I'll post the code.
Write a Custom exception example
Contents: Com.magic.rent.exception.custom, file name: Businessexception.java
1 PackageCom.magic.rent.exception.custom;2 3 Public classBusinessexceptionextendsRuntimeException {4 Publicbusinessexception (String message) {5 Super(message);6 }7 8 Publicbusinessexception (String message, throwable cause) {9 Super(message, cause);Ten } One}
In fact, there is no content, inheritance, and then a copy of the method is good. Of course, depending on your needs, you can add content yourself. It is also very easy to use, such as the code snippet below. Do not meet the criteria, throw an exception, and then constantly through the method to throw up the try-catch, and finally will be captured by the exception controller.
// throws an exception if the user information is not found if NULL ) { thrownew usernamenotfoundexception ( " Userdetailsservice.usernotfount "); }
"Javaweb" Spring+springmvc+mybatis+springsecurity+ehcache+jcaptcha Complete Web foundation Framework (III)