1.新建项目springmvc-lea,创建module mvcdemo
module:模块项目
解释: 这个是为了多项目管理的,比如,A,B 两个项目, B项目是A的子项目,需要使用A项目的类就可以建module project, 将B 依赖于A,这时候发布B的时候,会自动将A打成jar包
2.在WEB-INF里创建文件夹lib,把项目所需的包复制进去
3.打开Project Structure,选择Modules,Dependencies,点击右边的“+”号,选择“Jars or Directories ”,选择刚刚创建的lib文件夹。如果已经加入则提个勾,表示选中。最后OK即可。
4.在WEB-INF目录下创建 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
</web-app>
其中,两处servlet-name的名称相同,同时新建tomcat后选择Deployment,Application context,设置为/sprinfmvc,表示访问的地址为/springmvc
5.(方法一)在WEB-INF目录下创建 springmvc-servlet.xml
springmvc-servlet.xml 与上一步中的
<servlet-name>springmvc</servlet-name>
对应
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="simpleUrlHandlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index">indexController</prop>
</props>
</property>
</bean>
<bean id="indexController" class="controller.IndexController"></bean>
</beans>
这里class选择controller.IndexController,使用indexController作为别名,访问的地址为/index,交给indexController处理
6.(方法一)控制类IndexController
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class IndexController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("index.jsp");
mav.addObject("message", "Hello Spring MVC");
return mav;
}
}
控制类 IndexController实现接口Controller ,提供方法handleRequest处理请求
SpringMVC通过 ModelAndView 对象把模型和视图结合在一起
表示视图是index.jsp
模型数据是 message,内容是 “Hello Spring MVC”
7.(方法一)新建index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<h1>${message}</h1>
8.(方法一)运行,访问http://localhost:8080/springmvc/index
4.(方法二使用标签)在WEB-INF目录下创建 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_3_1.xsd"
version="3.1">
<display-name>Spring MVC Form Handling</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>springmvc-servlet.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>
</web-app>
5.(方法二)在WEB-INF目录下创建 springmvc-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 http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="test"></context:component-scan>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
</beans>
<context:component-scan base-package="test">
表示将base-package包或者子包下的所有的进行java类进行扫描,并把匹配的java类注册成bean。
6.(方法二)新建包test和控制类testController
package test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/test")
public class testController {
@RequestMapping("/say")
@ResponseBody
public void say(){
System.out.println("11111111111111");
}
}
@Controller标签表示告诉服务器 这个类是MVC中的C 这个类可以接收用户请求 处理用户请求
@RequestMapping标签用于地址映射
7.(方法二)运行
http://localhost:8080/springmvc/test/say
由于没有写网页,最后在控制台输出结果