白发戴花君莫笑,岁月从不败美人。 愿,像你我一如既往地好看。
上一章简单介绍了SpringMVC的运行流程(一),如果没有看过,请观看上一章。
一. SpringMVC的简单开发
SpringMVC 是Spring 框架里面的一个模块,如果不了解Spring, 可以参考老蝴蝶以前写的Spring系列文章。 故jar包一般用的都是Spring的jar包。
一.一 . 导入jar 包
需要导入 spring 的jar包, 和关于springmvc的jar包。 不要忘记导入jstl.jar 和standard.jar
一.二 在web.xml 配置前端控制器 DispatcherServlet
<!-- 配置springmvc的前端控制器,使用默认的位置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--默认的位置在 WEB-INF 下面,并且名称必须是 servlet-name-servlet.xml-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
springmvc 的配置文件 的位置固定,名称也必须固定。
一.三 配置 springMVC-servlet.xml 文件
因为 servlet-name 为springMVC, 故 springmvc的配置文件的名称必须为 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置action,前端访问路径为hello.action -->
<bean id="helloAction" name="/hello.action" class="com.yjl.action.HelloAction"></bean>
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
要配置相应的 映射器,适配器和 视图解析器, SpringMVC已经提供好了。 只需要引入即可。
一.四 开发Action. 要实现 Controller 接口
package com.yjl.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
@atuhor:yuejl
@Description: 类描述
*/
public class HelloActionF2 implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest paramHttpServletRequest,
HttpServletResponse paramHttpServletResponse) throws Exception {
System.out.println("你好,两个蝴蝶飞");
return null;
}
}
一.五 运行测试
输入网址, 为http://localhost:8090/SpringMVC01/user.action 观察控制台:
一个基本的SpringMVC 的程序就算是写好了。 但是,有很多需要改进的地方。
二. springmvc 配置文件位置和名称 改进
在web.xml 配置的时候, springmvc的名称和位置已经固定了,这个是不太友好的。
应该利用 contextConfigLocation 属性进行修改。
<!-- 配置前端控制器,自定义名称和位置 -->
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
这样,就可以进行自定义了。
其中, contextConfigLocation 属性在 其父类 FrameworkServlet 里面。
改变后的位置为:
内容不变。
在HelloAction 类的方法中,进行输出,还是刚才的那个方法。
@Override
public ModelAndView handleRequest(HttpServletRequest paramHttpServletRequest,
HttpServletResponse paramHttpServletResponse) throws Exception {
//System.out.println("你好,两个蝴蝶飞");
System.out.println("你好,两个蝴蝶飞,自定义位置版本");
return null;
}
控制台进行输出:
三. 非注解形式的springMVC 配置
三.一 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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置的是非注解的 -->
<!-- 配置前端的 -->
<bean id="helloAction" name="/hello.action" class="com.yjl.action.HelloAction"></bean>
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 可以路径,映射到 helloAction. 用hello1.action 和hello2.action 访问均可 -->
<prop key="/hello1.action">helloAction</prop>
<prop key="/hello2.action">helloAction</prop>
</props>
</property>
</bean>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
三.二 HelloAction 类实现 HttpRequestHandler 接口。
HelloAction 中,这个时候,就不实现 Controller 接口了, 而是实现 HttpRequestHandler 的接口。
package com.yjl.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.HttpRequestHandler;
/**
@atuhor:yuejl
@Description: 类描述
*/
public class HelloActionF2 implements HttpRequestHandler{
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse reponse) throws ServletException, IOException {
// TODO 自动生成的方法存根
System.out.println("你好,两个蝴蝶飞,非注解的");
}
}
三.三 运行测试
输入网址,hello1访问: http://localhost:8090/SpringMVC01/hello1.action
控制台输出:
输入网址,hello2访问 : http://localhost:8090/SpringMVC01/hello2.action
注意,这个时候是不能通过 hello.action 进行访问的。 如果以 hello.action 进行访问,这个时候 会出现问题。
404, 路径输入错误。
四. 注解形式的SpringMVC的配置
上面 HelloAction 类必须实现 Controller 或者是 HttpRequestHandler 接口,重写里面的方法, 这样是不太好的。
故一般都是使用 注解进行配置开发。
四.一 SpringMVC 注解配置
处理器映射喊叫,处理器适配器,又换名字了。
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置的是注解的 -->
<!-- 扫描哪个基本包,多个用,进行分开 -->
<context:component-scan base-package="com.yjl.action"></context:component-scan>
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean>
</beans>
四.二 HelloAction的注解形式开发
package com.yjl.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
@atuhor:yuejl
@Description: 类描述
*/
@Controller //Controller注解放置在类上
public class HelloAction {
@RequestMapping("/hello.action")
public void hello(){
System.out.println("你好,两个蝴蝶飞,注解形式");
}
}
四.三 测试运行
输入网址, 访问为 hello.action
控制台输出:
然而页面上却并没有展示任何东西。 显示 404, 这个是正常的。 因为没有配置页面
五. 处理器映射器和处理器适配器简写
五.一 将映射器和适配器省略
上面注解形式时, 处理器映射器和处理器适配器 进行进行相应的简写,并且 在实际开发中也常常是简写的形式。
将这两句话:
<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
用下面的一句话进行代替
<!-- 适配器和映射器简写 -->
<mvc:annotation-driven></mvc:annotation-driven>
注意,不要忘记添加 mvc的约束。
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
<!--mvc的约束-->
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置的是注解的,重写视图解析器 -->
<context:component-scan base-package="com.yjl.action"></context:component-scan>
<!-- 适配器和映射器简写 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
五.二 输出方法测试
@RequestMapping("/hello.action")
public String hello(){
//System.out.println("你好,两个蝴蝶飞,注解形式");
System.out.println("你好,两个蝴蝶飞,简单注解形式");
return null;
}
五.三 测试运行
输入网址, 访问为 hello.action
六. 视图解析器 添加前缀和后缀
一般返回的结果,并不是 null, 可以为String (返回的为视图名), 可以为ModelAndView . 视图一般都放置在WEB-INF /jsp 文件下面,后缀都是 .jsp 。 故常常在视图解析器里面先定义好 前缀和后缀, 在返回视图名时,直接写最重要的那部分即可。
六.一 配置视图解析器
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置的是注解的,重写视图解析器 -->
<context:component-scan base-package="com.yjl.action"></context:component-scan>
<!-- 适配器和映射器简写 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
其中,prefix 与suffix 是视图解析器类里面的属性名。
六.二 在web-inf 下创建 jsp ,里面放置 list.jsp 视图
list.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>展示</title>
</head>
<body>
两个蝴蝶飞,你好啊,你好啊
</body>
</html>
六.三 HelloAction 方法,返回视图名
package com.yjl.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
@atuhor:yuejl
@Description: 类描述
*/
@Controller //Controller注解放置在类上
public class HelloAction {
@RequestMapping("/hello.action")
public String hello(){
System.out.println("你好,两个蝴蝶飞,视图解析器前缀和后缀形式");
return "list";
}
}
六.四 测试运行
输入网址, 访问为 hello.action
控制台展示:
前台展示:
七. 标准常用配置
七.一 web.xml
<!-- 配置前端控制器,自定义名称和位置 -->
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
七.二 springmvc 配置文件
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置的是注解的,重写视图解析器 -->
<context:component-scan base-package="com.yjl.action"></context:component-scan>
<!-- 适配器和映射器简写 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
七.三 HelloAction 控制类
package com.yjl.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
@atuhor:yuejl
@Description: 类描述
*/
@Controller //Controller注解放置在类上
public class HelloAction {
@RequestMapping("/hello.action")
public String hello(){
System.out.println("你好,两个蝴蝶飞,视图解析器前缀和后缀形式");
return "list";
}
}
谢谢!!!