*编译环境:
Jdk:jdk1.8
Eclipse
Tomcat:apache-tomcat-7
spring:4.2.4
注意:web.xml会在第一行报错,这是你的jar包版本与编译环境不一致,报no-Mappering…url是没有找到Controller注解下的类,也就是没有注入到容器中,调bug可以在左下角copy trace来看报错日志,以及在浏览器访问时,注意url的写法是否正确
1.开发步骤
1.1首先将所有的编码方式调成UTF-8编码方式,否则找不到映射文件,还以为配置写错了。
1.2.创建Dynamic web项目:springmvc01,配置好tomcat,以便项目发布
结构目录如下:其中config是Resourcefolder,便于识别目录下的文件路径
1.3导入springmvc的jar包,导入至WebContent/WEB-INF/lib下面,会自动Buildpath,不用手动shift+鼠标全选添加
jar包:
1.4编写TestController类
@Controller
public class HelloController {
@RequestMapping("hello")
public ModelAndView hello(){
System.out.println("hello springmvc....");
//创建ModelAndView对象
ModelAndView mav = new ModelAndView();
//设置模型数据
mav.addObject("msg", "hello springmvc...");
//设置视图名字
mav.setViewName("/WEB-INF/jsp/hello.jsp");
return mav;
}
}
1.5创建hello.jsp页面,注意编码格式是不是UTF-8,由前端控制器传入到页面显示 : ${ msg }表达式
<%@ 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>输出提示 入门程序</title>
</head>
<body>
${ msg }
</body>
</html>
1.6创建与配置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:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置controller扫描包 -->
<context:component-scan base-package="com.itheima.springmvc.controller" />
</beans>
1.7在web.xml中配置核心控制器和核心配置文件路径及访问url
<?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>01-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>
<!-- 加载springmvc核心配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
1.8启动项目发布到tomcat通过浏览器测试,http://localhost:8080/01-springmvc2/hello.action
1.9小结-springmvc代码执行流程
这是入门案例,作为小白还是踩了许多坑…