最近需要用到
spring mvc
这个
web
框架,学习了相关的资料,也查看了好些帖子,都介绍的比较详细,但没有适合自己的模板,在看《
s
pring in action
》的时候,(PS
:蛮好的
spring
书籍),结合了里面对各种控制器和视图解析器的介绍,个人决定采用
SimpleUrlHandlerMapping
控制器+
PropertiesMethodNameResolver
的视图解析的方式。
下面介绍一下自己 总结的mvc
处理流程:
1.
首先浏览器发送一个
url请求,比如
http://www.xxx.com/hello.html
2.
然后被
org.springframework.web.servlet.DispatcherServlet
拦截,
DipatcherServlet
根据配置里的xml
文件路径找到相应的配置文件,比如
dispatcher-servlet.xml
文件。
DipatcherServlet
在web.xml
中配置。如果使用的控制器映射方式是
SimpleUrlHandlerMapping
(推荐使用这种,映 射方式有
3
种:
BeanNameUrlHandlerMapping
,
SimpleUrlHandlerMappin
,
CommonsPathMapHandlerMapping
),默认的控制器映射方式是
BeanNameUrlHandlerMapping
。在
SimpleUrlHandlerMapping
中的
mappings
中查找“
/hello*.html
”的
key
值,然后找相应的
value
,即对应
bean
的
id
名字,比如
helloController
。然后在配置文件中查找相应
bean
的
id
为“
helloController
”的
bean
配置,查找其对应的处理类,比如“
com.ctq.action.HelloController
”。
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello*.html">helloController</prop>
</props>
</property>
</bean>
比如
<bean id="helloController" class="com.ctq.action.HelloController">
现在由
HelloController
类来进行相应的处理操作(推荐
HelloController
继承
MultiActionController
),
3.
然后
D
ipatcherServlet询问配置文件中的视图解析器
PropertiesMethodNameResolver
(个人推荐用这种,共有
3
种:
InternalResourceViewResolver
,
ParameterMethodNameResolver
,
PropertiesMethodNameResolver
。我觉得后两种都还可以,第一种虽然为默认情况,但适合小型简单的应用)。若采用这种视图解析器,需要在第
3
步中的
bean
配置中加入
methodNameResolver
属性,以代替默认的
InternalResourceViewResolver
配置。比如:
<bean id="helloController" class="com.ctq.action.HelloController">
<property name="methodNameResolver">
<ref bean="methodNameResolver"/>
</property>
</bean>
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/hello.html">hello</prop>
<prop key="/helloworld.html">helloWorld</prop>
<!-- 可以有多个 -->
</props>
</property>
</bean>
这样就会找到相应的hello.jsp
页面了,将页面由服务器解析给浏览器用户。如果用户输入的
url
地址为:
http://www.xxx.com/helloworld.html
,则会被
HelloController
的
helloWorld()
方法执行。
要点:这里要注意的是,web.xml
中的过滤路径不能为“
/*
”,应当改为
*.html
或者其他的形式,在
SimpleUrlHandlerMapping
的配置中,设置为“
/hello*.html
”这里表示只要以
hello
开头,以“
.html
”结尾的
url
地址形式都由相对应的
helloController
来处理,然后转向
PropertiesMethodNameResolver
中,对于具体的“
/hello.html
”形式的
url
由控制器中的
hello
方法来执行,对于形式为“
/helloworld.html
”形式的
url
地址由其中的
helloWorld
方法来执行。然后根据方法中定义的视图转向对应的处理页面。
下面附上我的例子代码:
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" 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>spring-mvc</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-list>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
这里的contextConfigLocation可以指定相应路径下的配置文件,多个文件可以之间用逗号隔开。
dispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<bean id="simpleUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello*.html">helloController</prop>
</props>
</property>
</bean>
<bean id="helloController" class="com.ctq.action.HelloController">
<property name="message">
<value>Hello Relic~~~~!!</value>
</property>
<property name="methodNameResolver">
<ref bean="methodNameResolver" />
</property>
</bean>
<bean id="methodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/hello.html">hello</prop>
<prop key="/helloworld.html">helloWorld</prop>
<!-- 可以有多个 -->
</props>
</property>
</bean>
</beans>
HelloController.java
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 HelloController extends MultiActionController {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ModelAndView hello(HttpServletRequest req, HttpServletResponse res)
throws Exception {
System.out.println("http://www.xxx.com/hello.html调用的是hello方法");
return new ModelAndView("/jsp/hello.jsp", "message", message); //返回jsp文件夹中的hello.jsp
}
public ModelAndView helloWorld(HttpServletRequest req,
HttpServletResponse res) {
System.out.println("http://www.xxx.com/helloworld.html调用的是helloWorld方法");
return new ModelAndView("/jsp/hello.jsp", "message", message);
}
}
hello.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>hello</title>
</head>
<body>
<h1>WELCOME Index</h1>
<h2>Hello</h2>
<h2>${message}</h2>
</body>
</html>
我总结的方式是SimpleUrlHandlerMapping+
PropertiesMethodNameResolver的方式,至于这种方式的优缺点,相关的介绍有很多,也可以找相关的书籍(我是在spring in action中了解的)。
分享到:
相关推荐
第4篇是Spring MVC与MyBatis的项目整合实战,通过对水果网络销售平台的需求分析、功能设计、数据库设计以及模块详细编码实现,让读者了解整合项目开发的整体流程。 《Spring MVC+MyBatis开发从入门到项目实战》对...
此资源是详细描述spring mvc的执行过程
主要给大家介绍了关于Spring MVC请求参数与响应结果全局加密和解密的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Spring MVC的工作流程如下: 用户通过视图层发送请求到服务器。 在服务器中请求被Controller接收,Controller调用相应的Model层处理请求,处理完毕后结果返回到Controller。 Controller再根据请求处理的结果找到...
《深入解析Spring MVCgn Web Flow》出自Spring核心开发者之手,不仅详细分析代码,全面剖析了两个框架的各种特性(包括一些不为人知的技术亮点)。告诉读者如何最大程度地发挥出它们的潜力。还解密了设计这两个框架时...
详细介绍了springMVC搭建流程,以及springMVC技术详解
主要给大家介绍了关于Spring MVC处理参数中的枚举类型通用实现方法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。 具体请参考第二章的图2-1。 ...
请求处理流程: 我们将详细解释Spring MVC中的请求处理流程,包括请求映射、参数绑定、数据验证和响应生成。这有助于您理解整个请求生命周期。 配置与注解: 我们将演示如何配置Spring MVC,包括XML配置和注解驱动...
第六章 注解式控制器详解1(注解式控制器运行流程及处理器定义).pdf 第六章 注解式控制器详解2(SpringMVC3强大的请求映射规则详解).pdf 第六章 注解式控制器详解3(生产者、消费者请求限定).pdf 第六章 注解式控制器...
任何系统都要遵循系统设计的基本流程,本系统也不例外,同样需要经过市场调研,需求分析,概要设计,详细设计,编码,测试这些步骤,基于java技术、ssm框架、B/S机构、Mysql数据库设计并实现了自助购药小程序。...
任何系统都要遵循系统设计的基本流程,本系统也不例外,同样需要经过市场调研,需求分析,概要设计,详细设计,编码,测试这些步骤,基于java语言设计并实现了面向企事业单位的项目申报小程序。该系统基于B/S即所谓...
主要介绍了Spring mvc Json处理实现流程代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
MD文档:详细介绍了每个项目的需求分析、系统设计、系统实现和测试等环节,让您能够全面了解项目的开发流程和关键技术。此外,还附带了详细的API文档,方便您查阅各个功能模块的接口和参数说明。 笔记资料:整理了...
前面学习了简单的Spring Web知识,接着学习更高阶的Web技术。下面这篇文章主要给大家介绍了spring mvc高级技术的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起看看吧
包括摘要,背景意义,论文结构安排,开发技术介绍,需求分析,可行性分析,功能分析,业务流程分析,数据库设计,er图,数据字典,数据流图,详细设计,系统截图,测试,总结,致谢,参考文献。
3. Spring 整合JMS和ActiveMq流程 1) 下载和部署ActiveMq服务器 2) Spring jms和activemq相关依赖引入 3) Spring整合activemq配置 4) 定义消息发布者(生产者) 5) 定义消息订阅者(消费者) 6) Spring mvc配置 7) 实例...
第15章:对Spring MVC框架进行详细介绍,对REST风格编程方式进行重点讲解,同时还对Spring 3.0的校验和格式化框架如果和Spring MVC整合进行讲解。 第16章:有别于一般书籍的单元测试内容,本书以当前最具实战的...
主要给大家总结介绍了关于Spring MVC请求参数接收的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧