JAVA entry-level [5]-initial setup of SpringMVC, javaspringmvc
1. Create a Module
1. Create a Module, as shown in the following figure:
<Dependency> <groupId> org. springframework </groupId> <artifactId> spring-webmvc </artifactId> <version> 4.3.5.RELEASE </version> </dependency>2. Configure web. xml
Spring MVC comes with a Dispatcher Servlet, whose full name is org. springframework. web. servlet. DispatcherServlet.
1. Configure the node servlet and servlet-mapping in web. xml:
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
2. Description:
- If url-pattern is/, all requests are mapped to DispatcherServlet.
- As agreed, the file named {servletname}-servlet. xml under the WEB-INF is automatically searched at initialization, which corresponds to the springmvc-servlet.xml in this example.
3. If you want to put it in another directory, you need to set the init-param node in the servlet. param-name must be contextConfigLocation, And param-value must be the target path. For example, we want to put the spring configuration file in/WEB-INF/config/springmvc. xml, you can configure as follows:
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet>
In this project, we use the first method.
Iii. controller and view
The MVC framework consists of model, view, and controller. The model is generally a few basic Java Beans. The controller is used to process website requests, and the view is used to display corresponding pages.
1. controller
Create a new package named com. cathy. controller in src \ main \ java, and then create the class CategroyController. java.
@Controllerpublic class CategoryController { @RequestMapping(value = "/category/edit") public String Edit() { return "edit"; } @RequestMapping("/category/detail") public String Detail(){ return "detail"; }}
Notes:
① @ Controller annotation: The annotation method can be used to clearly define the class as the Controller class for processing requests;
② @ RequestMapping () annotation: used to define a request ing. value is the request url;
③ Return "edit": the page returned after processing the request. The edit. jsp page is returned for this request.
2. view
Create two jsp pages in the WEB-INF/jsp/directory: edit. jsp and detail. jsp
3. Configure springmvc-servlet.xml
Go back to the mvc-dispatcher-servlet.xml for configuration. Add the component-scan label, specify the controller package, and scan the Annotation
<context:component-scan base-package="com.cathy.controller"></context:component-scan><mvc:annotation-driven></mvc:annotation-driven>
Then configure ViewResolver
<!--View Resolver--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/></bean>
3. Problems:
① Running error:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' available
② Solution:
Modify springmvc-servlet.xml
xmlns:mvc="http://www.springframework.org/schema/cache
Change
xmlns:mvc="http://www.springframework.org/schema/mvc
Xsi is also modified accordingly
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"
③ The complete xml file is as follows:
<? 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: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context h Ttp: // www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <! -- Controller package --> <context: component-scan base-package = "com. cathy. controller"> </context: component-scan> <! -- Enable annotation --> <! -- <Mvc: annotation-driven> </mvc: annotation-driven> --> <! -- View Resolver --> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp/"/> <property name =" suffix "value = ". jsp "/> </bean> </beans>Springmvc-servlet.xml 4. Operation
Follow the previous JAVA entry [4]-IntelliJ IDEA to configure Tomcat to Run Configuration, and then Run ctrl + f5 to test the url:
Http: // localhost: 8091/category/detail/
Http: /localhost: 8091/category/edit/