springMVC环境搭建
1、jar包 放在WEB-INF下的lib文件夹下
2、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置文件 前端控制器的核心配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
1: *.action *.do 拦截所有以.action .do结尾的请求 企业 后台开发 应用此种方式
2: /* 拦截所有(包含.jsp .js .css .png.jpg...) 全拦截 企业不用
3: / 拦截所有(包含.js .css .png .jpg) (不包含.jsp) 全拦截 另行 对静态资源放行配置 企业 前台应用使用此种 方式
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
其中<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
使用的contextConfigLocation和加载spring的配置文件使用的是同一个,这是由于spring和springmvc共用同一个容器,加载配置文件加载到同一个地方。
3、springmvc.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 扫描 扫描@Controller注解-->
<context:component-scan base-package="cn.zj.controller"/>
</beans>
4、DemoController.java
package cn.zj.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class DemoController {
@RequestMapping(value="/demo.action")
public ModelAndView demo(){
ModelAndView modelAndView = new ModelAndView();
// 数据
// 视图
modelAndView.setViewName("/WEB-INF/jsp/index.jsp");
return modelAndView;
}
}
5、index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
springmvc测试
</body>
</html>
6、启动服务器后,在浏览器输入地址