SpringMVC-a simple example: springmvc
1. Import the jar package
2. Configure the web. xml file
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param></servlet><servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern></servlet-mapping>
Note:
The core processor class org. springframework. web. servlet. DispatcherServlet of SpringMVC is configured. url-pattern specifies the request to be filtered.
Note that DispatcherServlet loads the SpringMVC Config file for/WEB-INF/servletName-servler.xml by default, and its location and name can be modified through initialization parameters.
3. Add SpringMVC Config file (springmvc-servlet.xml)
<context:component-scan base-package="com.nucsoft.springmvc.handler"/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/></bean>
Note:
(1) Specify the base package to be scanned
(2) Specify the view Parser: prefix + viewName + suffix
4. Add Target classes
/** * @author solverpeng * @create 2016-08-01-19:19 */@Controllerpublic class HelloWorldHandler { @RequestMapping("/hello") public String hello() { System.out.println("hello world..."); return "hello"; }}
Note:
(1) The request processing class must be in the IOC container
(2) @ RequestMapping is used to map requests. The value Attribute specifies the mapped url. It is equivalent to namespace.
(3) The returned value is eventually parsed as a ModelAndView object. Return to the view with the view parser.
5. SpringMVC, as a presentation layer framework, is another excellent framework after Stuts2 and is supported by the entire Spring system. It feels more elegant to use.
When learning spring MVC, you must understand its core processor and the issues to be solved by the presentation layer,
For example: obtain native Servlet resource problems, ing problems, parameter acquisition problems, model injection problems, formatting problems, Data Validation Problems, return value processing problems, view rendering problems, prevent form repeated submission problems, files upload and download problems.
SpringMVC's entire running process and request lifecycle issues. Can we achieve what we want through a custom method, but SpringMVC has not yet solved the problem.
What are the advantages and disadvantages of Struts2.
To learn SpringMVC, you must understand the above problems.