1、利用IDEA新建一个普通的maven项目,通过添加框架支持,使普通的maven项目变成Javaweb项目。 2、在pom.xml中添加依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
3、配置web.xml,注册DispatcherServlet
<?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">
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatcherServlet要绑定spring的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet.xml</param-value>
</init-param>
<!-- 表示容器再启动时立即加载dispatcherServlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--所有的url请求都会通过DispatcherServlet-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4、编写spring的配置文件dispatcherServlet.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置处理器映射器,根据url找到duiyid的bean,返回前端控制器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 配置处理器适配器,收到前端控制器的handle,找到对应的Contrller -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 配置视图解析器,收到前端控制器的modelview,返回解析后的view名-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 自动补充前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 自动补充后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5、编写HelloController实现Controller接口
public class HelloController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","hello spring mvc");
modelAndView.setViewName("hello");
return modelAndView;
}
}
6、在spring的配置文件dispatcherServlet.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置处理器映射器,根据url找到duiyid的bean,返回前端控制器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 配置处理器适配器,收到前端控制器的handle,找到对应的Contrller -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- 配置视图解析器,收到前端控制器的modelview,返回解析后的view名-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 自动补充前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 自动补充后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--handle,注册bean-->
<bean id="/hello" class="com.wen.controller.HelloController"/>
</beans>
7、编写要跳转的jsp页面,显示modelview存放的数据。以及我们的正常页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
${msg}
</body>
</html>
8、配置tomcat进行测试
|