Spring entry (5) [springMVC environment setup], springspringmvc

Source: Internet
Author: User

Spring entry (5) [springMVC environment setup], springspringmvc

SpringMVC, as a WEB component of spring, is a MVC idea that reduces the difficulty of WEB development. The establishment of springMVC environment is introduced. The specific principles are described later. Anyone who has used the framework knows that to use a framework in a WEB project, this framework must be introduced. Like other frameworks, springMVC is introduced through DispatcherServlet, then we need to go to the web. configure this servlet in xml. In the lib directory, I have imported all the jar packages used. view my web. xml file configuration,

<? 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" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0"> <display-name> springmvc </display-name> <welcome-file-list> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index. jsp </Welcome-file> <welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file> <welcome-file> default. jsp </welcome-file> </welcome-file-list> <! -- Log4j configuration file --> <context-param> <param-name> log4jConfigLocation </param-name> <param-value>/WEB-INF/classes/log4j. properties </param-value> </context-param> <! -- Spring configuration file --> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: application-context.xml </param-value> </context-param> <! -- Configure springmvc --> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <! -- Springmvc configuration file --> <init-param> <param-name> contextConfigLocation </param-name> <param-value>/WEB-INF/classes/springmvc. 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> <! -- Configure spring --> <listener-class> org. springframework. web. context. ContextLoaderListener </listener-class> </listener> <! -- Configure logs in the system --> <listener-class> org. springframework. web. util. Log4jConfigListener </listener-class> </listener> </web-app>

SpringMVC

In the configuration file we configured DispatherServlet, and configured its initialization parameter contextConfigLocation, specifying the path of the file to:/WEB-INF/classes; configured with the intercepted URL, all URLs are intercepted here.

Spring

You may wonder why spring is configured here. Isn't springmvc alone unavailable? The answer is yes. We can put all the configurations in the springmvc configuration file, but here we follow the hierarchical configuration to make springMVC a configuration file, spring is a configuration file that separates the functions they want to complete. In fact, all the configurations can be loaded by dispatherServlet.

The spring loading method uses a listener, ContextLoaderListener, which reads the configuration parameter contextConfigLocation in the context, reads the spring configuration file, and loads the spring container.

Log4j

Because the log function is required in the project, logs are configured here and the configuration file in context is referenced.

The following describes the springmvc configuration file,

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/beans ht Tp: // www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springfr Amework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! -- If the component scan is configured, you do not need to configure <context: annotation-config> because the component scan already contains the following annotations: @ Controller, @ Service, @ Resposity, @ Component, @ Controller annotation must be scanned by dispatherDispatcherServlet, and dependency injection is also implemented. @ Autowired and @ Resource support --> <context: component-scan base-package = "com.cn. my "> <context: include-filter type =" annotation "expression =" org. springframework. stereotype. controller "/> </context: component-scan> <! -- Enable annotation-based driver --> <context: annotation-config> </context: annotation-config> <! -- Enable the annotation driver of mvc. You can use the @ RequestParam annotation to help request parameters to Controller Parameters --> <mvc: annotation-driven/> <mvc: resources location = "/resource/*" mapping = "/resource/**"/> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean> </beans>

The above is the springmvc configuration file. First, Component scanning is configured. The function of Component scanning is to automatically perform class-level annotations (@ Controller, @ Component, @ Service, @ Resposity ), only the @ Controller annotation class is configured here, because classes with @ Controller annotation can only be loaded by DispatherServlet, and then the annotation-based driver is enabled, which can implement dependency injection, for @ Autowired and @ Resource annotations, you can skip this configuration because this function is already included in the component scan. The mvc-based annotation driver, you can easily bind the request parameters to the controller parameters.
The view parser is configured at the bottom. The default view parser is used here.

 

Let's look at the spring configuration file,

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" x Si: schemaLocation = "http://www.springframework.org/schema/beans http: // w Http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.or G/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <context: component-scan base-package =" com.cn. my "> <context: exclude-filter type =" annotation "expression =" org. springframework. stereotype. controller "/> </context: component-scan> <! -- Configure a data source and use the data source provided by spring --> <bean id = "dataSource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" com. mysql. jdbc. driver "> </property> <property name =" url "value =" jdbc: mysql: // 127.0.0.1: 3306/test "> </property> <property name =" username "value =" root "> </property> <property name =" password "value =" 123456 "> </property> </bean> <bean id = "jdbcTemplate" class = "org. springframework. jdbc. core. jdbcTemplate "> <property name =" dataSource "ref =" dataSource "> </property> </bean> </beans>

Spring can be used to configure the data source, inject the dependencies of the transaction or service layer. First, it is a component scan. The scanned object is a data source in addition to the annotation of @ Controller, the data source provided by spring is used here. Other methods such as DBCP, C3P0, and JNDI can also be used, which will be discussed later.

Then, a data access object JdbcTemplate is configured to operate the database.

So far, our environment has been set up. The following describes how to use it,

Compile the Controller class,

Because we use component scanning, we use annotation @ Controller on the class.

Package com.cn. my. controllor; import javax. servlet. http. httpServletRequest; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. pathVariable; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestParam; import com.cn. my. service. courseServiceInter; @ Controller @ RequestMapping ("/my/*") public class MyController {@ Autowired private CourseServiceInter csi; // url of the general type @ RequestMapping ("my ") public String method (HttpServletRequest request, @ RequestParam ("courseId") String courseid) {csi. findCourse (courseid); return "success";} // restful URL // http: // localhost: 8080/my/my2/12 @ RequestMapping ("my2/{courseId}/{name}") public String method2 (@ PathVariable ("courseId") String courseId, @ PathVariable ("name") String name) {csi. findCourse (courseId + "" + name); return "success ";}}

In addition to the @ Controller annotation used in the class, the @ RequestMapping annotation is used to indicate the access path, and then the @ RequestMapping annotation is used in the method, A complete url access path consists of the path on the class path plus method, for example:/my. do: This is an access path. We also use a findCourse method on the service layer. The objects on the service layer have framework dependency injection. The @ RequestParam annotation is used in the method, this annotation can bond the parameters in the url to the parameters of the method. The value in @ RequestParam must be the same as the request parameter name in the url, followed by the parameter name in the corresponding method. The method returns a value of the String type. Here the logical view name is returned, or a JSON String can be returned. This method is described later.

After the controller is configured, you can access it. The sprigMVC environment and controller compilation are complete.

If any error exists, thank you!

 

Related Article

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.