本文让大家迅速掌握springMVC的使用方法,以最简单的方式理解此框架
一、用eclipse新建一个web项目,命名为springMVC1,tomcat的端口号为9090
二、在WEB-INF目录下的lib文件夹中添加所需要的jar包
上面所添加的jar包,只要从网上下载都是都有的
三、编辑web.xml文件,在WEB-INF目录下
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>springMVC1</display-name>
<!-- 标签<welcome-file>就是直接访问http://localhost:9090/springMVC1此项目时直接跳转到设置的页面-->
<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>
<!-- 下面的springMVC和DispatcherServlet就是设置让springMVC获取到用户的所有请求-->
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 下面的contextConfigLocation和spring-servlet.xml就是设置springMVC配置文件,来获取框架的上下文信息-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<!--一定要和上面配置的<servlet-name>名称一致,过滤上面获取的用户请求,下面设置就意味着接收所有的请求-->
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
四、编写控制器,在src目录下,新建包com.tgb.web.controller,在此包下新建java类HelloWorldController.java
package com.tgb.web.controller; import java.util.HashMap;
import java.util.Map; 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 HelloWorldController implements Controller { @Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
System.out.println("------这是控制方法-----");
Map<String,Object> map=new HashMap<String,Object>();
map.put("hello1", "hello1");
map.put("hello2", "hello2");
map.put("hello3", "hello3");
String hello = "你好啊";
String url="/welcome";
return new ModelAndView(url,"map",map);
}
}
根据springMVC规范HelloWorldController要实现接口Controller和方法,这里大家暂时先这么用,最原始的访问方法,后面会和大家说全部用注解的形式访问,实现的方法返回的是ModelAndView,它可以直接returna new ModelAndView(url,"map",map),url就是跳转的页面地址,map就是传递到jsp页面的数据对象
五、编写jsp页面,在WEB-INF目录下,新建jsp文件夹,新建welocme.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
这是我的第一个jsp页面!!!!!!!++++++++++
<br/>
<h>传递的数据</h>
${hello}
${map.hello2}
<div> <c:forEach items="${map}" var="m">
${m.key }======${m.value }<br/>
</c:forEach>
</div>
</body>
</html>
六、以上有了控制层和页面,那么我怎么让springMVC知道我的这些资源呢,并且能够请求?那么解决方法就是上上面提到的springMVC的上下文文件spring-servlet.xml,在src目录下,新建包config文件夹,然后创建spring-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!--name="/test1/helloworld" 就是要访问控制器的路径,然后进入这个控制方法-->
<bean name="/test1/helloworld" class="com.tgb.web.controller.HelloWorldController" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
七、部署编辑
直接在浏览器的地址栏里输入http://localhost:9090/springMVC1/test1/helloworld
八、控制器里包含多个方法
1.新建包含多个方法的控制器multiController.java
package com.tgb.web.controller; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class MultiController extends MultiActionController {
//定义add的方法,在url中访问http://localhost:9090/springMVC1/test1/multi?action=add
public ModelAndView add(HttpServletRequest request,HttpServletResponse respose){
System.out.println("add");
String method="add";
return new ModelAndView("/multi","method",method);
}
//定义update方法,在url中访问http://localhost:9090/springMVC1/test1/multi?action=update
public ModelAndView update(HttpServletRequest request,HttpServletResponse respose){
System.out.println("update");
String method="update";
return new ModelAndView("/multi","method",method);
} }
2.新建多方法跳转的jsp,multi.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
<h>控制器中多个方法跳转的页面</h>
本次方法是${method }
<br/>
<h>传递的数据</h>
${hello}
${map.hello2}
<div> <c:forEach items="${map}" var="m">
${m.key }======${m.value }<br/>
</c:forEach>
</div>
</body>
</html>
3.在spring-servlet.xml里添加springMVC多方法解析包
<bean name="/test1/multi" class="com.tgb.web.controller.MultiController" >
<property name="methodNameResolver">
<ref bean="paramMethodResolver"/>
</property>
</bean>
<!--加载springMVC的多方法解析包,标签<property>中name固定为paramName,value的值是可以改变的,在访问路径里需要用到,例如http://localhost:9090/springMVC1/test1/multi?action=add-->
<bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" >
<property name="paramName" value="action"></property>
</bean>