1.String conf= "Applicationcontext.xml";
ApplicationContext atc=new classpathxmlapplicationcontext (conf);
2. Specify the Scan class path
<context:component-scan base-package= "Com.tarena" ></context:component-scan>
3. Scan the db.perproties file configuration
<util:properties id= "Jdbcprops" location= "Classpath:db.properties" >
</util:properties>
4.DispatcherServlet controller configuration
<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:spring-mvc.xml </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</ Servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
< Url-pattern>*.form</url-pattern>
</servlet-mapping>
5.HandlerMapping components
<bean id= "handlermapping" class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<!--specify the request and controller correspondence--
<property name= "Mappings" ref= "urlmappings" ></property>
</bean>
<!--define request map Map--
<util:properties id= "urlmappings" >
<prop key= "/hello.form" >helloController</prop>
</util:properties>
6.ViewResolver components
<!--definition View resolver viewreslover
<bean id= "Viewreslover" class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/web-inf/jsp/" ></property>
<property name= "suffix" value= ". JSP" ></property>
</bean>
7. Chinese garbled solution
Characterencodingfilter Configuration Example
<!--handling coding problems--
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.form</url-pattern>
</filter-mapping>
8. Use Interceptors for login checks
Spring's handlermapping processor supports interceptor applications. When you need to provide special functionality for certain requests, such as authenticating a user,
is very suitable.
Interceptors must implement the Handlerinterceptor interface, which has the following 3 methods
Prehandle (...) Called before the processor is executed.
Posthandle (...) Called after the processor executes, before the view is processed. The model data can now be modelandview by the object
Processing or treating a view
Aftercompletion (...) Called after the entire request has been processed
9. The configuration of the custom interceptor is as follows
<!--append Someinterceptor-Custom Interceptor configuration-
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path= "/springmvc1/*"/>
<mvc:exclude-mapping path= "/login/*"/>
<bean class= "Com.tarena.interceptor.SomeInterceptor" ></bean>
</mvc:interceptor>
</mvc:interceptors>
10. Exception Handling
Spring MVC handles exceptions in the following three ways
Using the simple exception handler provided by spring MVC Simplemappingexceptionresolver
Implementing the Handlerexceptionresolver interface custom exception handler
Using @exceptionhandler annotations for exception handling
When using 11.SimpleMappingExceptionResolver, you only need to define it in spring's XML configuration file.
The definition example is as follows:
12. Error page Definition
<!--error page definition--
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
13. Turn on the configuration of @requestmapping annotations
<mvc:annotation-driven></mvc:annotation-driven>
14. Upload file Configuration
<!--Upload file configuration--
<bean id= "Multipartresolver"
class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name= "maxuploadsize" value= "51200" ></property>
<property name= "resolvelazily" value= "true" ></property>
</bean>
15. Uploading Files
@Controller
public class Uploadcontroller {
@RequestMapping ("/toupload.form")
Public String Toupload () {
return "Upload";
}
@RequestMapping (value= "/upload.form")
Public String Upload (@RequestParam (value= "file", Required=false) multipartfile file,httpservletrequest Req,modelmap Model) {
String path=req.getsession (). Getservletcontext (). Getrealpath ("Upload");
String Filename=file.getoriginalfilename ();
SYSTEM.OUT.PRINTLN (path);
File Targetfile=new file (path,filename);
if (!targetfile.exists ()) {
Targetfile.mkdirs ();
}
Save
try{
File.transferto (targetfile);
Model.addattribute ("FileUrl", Req.getcontextpath () + "/upload/" +filename);
}catch (Exception e) {
E.printstacktrace ();
}
return "result";
}
@RequestMapping (value= "/uploads.form")
Public String uploads (@RequestParam (value= "file", Required=false)
Multipartfile[] Files,httpservletrequest Req,modelmap model) {
List<string> urls=new arraylist<string> ();
for (Multipartfile file:files) {
String path=req.getsession (). Getservletcontext (). Getrealpath ("Upload");
String Filename=file.getoriginalfilename ();
SYSTEM.OUT.PRINTLN (path);
File Targetfile=new file (path, fileName);
if (!targetfile.exists ()) {
Targetfile.mkdirs ();
}
Save
try{
File.transferto (targetfile);
Urls.add (Req.getcontextpath () + "/upload/" +filename);
}catch (Exception e) {
E.printstacktrace ();
}
}
Model.addattribute ("Fileurls", URLs);
return "result";
}
}
Spring Common Code