1 HelloWorld
- SpringMVC运行流程:
步骤
- 导入jar包。
- 在web.xml中配置DispatcherServlet。
- 加入SpringMVC配置文件。
- 编写处理请求的处理器,并标识为处理器。
- 编写视图。
项目结构:
1、导入jar包
aopalliance-1.0.jar META-INF aopalliance commons-logging-1.2.jar spring-aop-5.2.3.RELEASE.jar spring-aspects-5.2.3.RELEASE.jar spring-beans-5.2.3.RELEASE.jar spring-context-5.2.3.RELEASE.jar spring-context-support-5.2.3.RELEASE.jar spring-core-5.2.3.RELEASE.jar spring-expression-5.2.3.RELEASE.jar spring-instrument-5.2.3.RELEASE.jar spring-jdbc-5.2.3.RELEASE.jar spring-jms-5.2.3.RELEASE.jar spring-messaging-5.2.3.RELEASE.jar spring-orm-5.2.3.RELEASE.jar spring-oxm-5.2.3.RELEASE.jar spring-test-5.2.3.RELEASE.jar spring-tx-5.2.3.RELEASE.jar spring-web-5.2.3.RELEASE.jar spring-webmvc-5.2.3.RELEASE.jar
2、配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--Spring配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--SpringMVC配置文件--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--配置SpringMVC配置文件的位置:/WEB-INF/<servlet-name>-servlet.xml为默认位置--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--映射请求,所有请求都交由dispatcher处理--> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、配置dispatcher-servlet.xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--配置SpringMVC扫描的包,设置为只扫描@Controller注解--> <context:component-scan base-package="com.xianhuii" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter> </context:component-scan> <!--配置视图解析器:如何把handler方法返回值解析为实际的物理视图--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4、编写Controller
package com.xianhuii; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HellWorld { /** * 1、使用@RequestMapping注解来映射请求的URL * 2、返回值会通过视图解析器解析为实际的物理视图。 * 对于InternalResourceViewResolver视图解析器,会做如下解析: * 通过prefix + returnVal + suffix,得到实际的物理视图,然后进行转发。 * /WEB-INF/views/success.jsp * @return */ @RequestMapping("/helloworld") public String hello() { System.out.println("helloworld"); return "success"; } }
5、编写视图
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <a href="/helloworld">Hello World</a> </body> </html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>成功</title> </head> <body> <h4>Success Page</h4> </body> </html>
6、小结
- web.xml(总配置文件):
- 配置Spring。
- 配置SpringMVC。
- 配置映射请求。
- dispatcher-servlet.xml(SpringMVC配置文件):
- 配置扫描@Controller。
- 配置视图解析器。
- Controller:
- 逻辑处理。
- 转发视图。