1> Strut2 Mapping to MVC
CONTROLLER—FILTERDISPATCHER
We’ll start with the controller. It seems to make more sense to start there when talkingabout web applications. In fact, the MVC variant used in Struts is often referred to as a front controller MVC. This means that the controller is out front and is the first component to act in the process- ing. You can easily see this in figure 1.3. The controller’s job is to map requests to actions. In a web application, the incoming HTTP requests can be thought of as com- mands that the user issues to the application. One of the fundamental tasks of a web application is routing these requests to the appropriate set of actions that should be taken within the application itself. This controller’s job is like that of a traffic cop or air traffic controller. In some ways, this work is administrative; it’s certainly not part of your core business logic.
The role of the controller is played by the Struts 2 FilterDispatcher. This impor- tant object is a servlet filter that inspects each incoming request to determine which Struts 2 action should handle the request. The framework handles all of the controller work for you. You just need to inform the framework which request URLs map to which of your actions. You can do this with XML-based configuration files or Java annotations. We’ll demonstrate both of these methods in the next chapter.
NOTE Struts 2 goes a long way toward the goal of zero-configuration web applica- tions. Zero-configuration aims at deriving all of an application’s meta- data, such as which URL maps to which action, from convention rather than configuration. The use of Java annotations plays an important role in this zero-configuration scheme. While zero-configuration has not quite been achieved, you can currently use annotations and conventions to drastically reduce XML-based configuration.
Chapter 2’s HelloWorld application will demonstrate both the general architecture and deployment details of Struts 2 web applications.
MODEL—ACTION
Looking at figure 1.3, it’s easy to see that the model is implemented by the Struts 2 action component. But what exactly is the model? I find the model the most nebulous
of the MVC triad. In some ways, the model is a black box that contains the guts of the application. Everything else is just user interface and wiring. The model is the thing itself. In more technical terms, the model is the internal state of the application. This state is composed of both the data model and the business logic. From the high-level black box view, the data and the business logic merge together into the monolithic state of the application. For instance, if you are logging in to an application, both busi- ness logic and data from the database will be involved in the authentication process. Most likely, the business logic will provide an authentication method that will take the username and password and verify them against some persisted data from the data- base. In this case, the data and the business logic combine to form one of two states, “authenticated” or “unauthenticated.” Neither the data on its own, nor the business logic on its own, can produce these states.
Bearing all of this in mind, a Struts 2 action serves two roles. First, an action is an encapsulation of the calls to business logic into a single unit of work. Second, the action serves as a locus of data transfer. It is too early to go into details, but we’ll treat the topic in great depth during the course of this book. At this point, consider that an application has any number of actions to handle whatever set of commands it exposes to the client. As seen in figure 1.3, the controller, after receiving the request, must con- sult its mappings and determine which of these actions should handle the request. Once it finds the appropriate action, the controller hands over control of the request processing to the action by invoking it. This invocation process, conducted by the framework, will both prepare the necessary data and execute the action’s business logic. When the action completes its work, it’ll be time to render a view back to the user who submitted the request. Toward this end, an action, upon completing its work, will forward the result to the Struts 2 view component. Let’s consider the result now.
VIEW—RESULT
The view is the presentation component of the MVC pattern. Looking back at figure 1.3, we see that the result returns the page to the web browser. This page is the user interface that presents a representation of the application’s state to the user. These are com- monly JSP pages, Velocity templates, or some other presentation-layer technology. While there are many choices for the view, the role of the view is clear-cut: it translates the state of the application into a visual presentation with which the user can interact. With rich clients and Ajax applications increasingly complicating the details of the view, it becomes even more important to have clean MVC separation of concerns. Good MVC lays the groundwork for easily managing the most complex front end.
NOTE One of the interesting aspects of Struts 2 is how well its clean architecture paves the way for new technologies and techniques. The Struts 2 result component is a good demonstration of this. The result provides a clean encapsulation of handing off control of the processing to another object that will write the response to the client. This makes it easy for alternative responses, such as XML snippets or XSLT transformations, to be inte- grated into the framework.
If you look back to figure 1.3, you can see that the action is responsible for choosing which result will render the response. The action can choose from any number of results. Common choices are between results that represent the semantic outcomes of the action’s processing, such as “success” and “error.” Struts 2 provides out-of-the-box support for using most common view-layer technologies as results. These include JSP, Velocity, FreeMarker, and XSLT. Better yet, the clean structure of the architecture ensures that more result types can be built to handle new types of responses.
Since this favored MVC pattern has been around for decades, try visualizing what the MVC playing field would look like if the players were in fact nicely separated yet connectible. When I explain this to my students, I call it the Reese’s peanut butter cup principle. Is this tasty treat chocolate or peanut butter? After your first bite, you dis- cover it’s both! How could you use this peanut butter if all you wanted was a PBJ sand- wich? And so it goes with technology: how do you get all the richness you desire without actually “combining” the ingredients? Grab some sweets and continue read- ing to learn about Struts 2 and the framework request-processing factory.
1> Strut2 Mapping to MVC的更多相关文章
- Strut2 和Spring MVC 文件上传对比
在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件 (http://commons.apache.org/proper/commo ...
- Spring MVC学习笔记——引入静态文件
1.在user-servlet.xml中加入以下代码,才能使得对静态文件的请求不被Controller捕获,而映射到一个固定的地址 <!-- 将静态文件指定到某个特殊的文件夹中统一处理 --&g ...
- Spring MVC的配置文件(XML)的几个经典案列
1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...
- spring mvc 第一天【注解实现springmvc的基本配置】
创建pojo,添加标识类的注解@Controller,亦可添加该Handler的命名空间:设置类的@RequestMapping(value="/hr") 该类中的方法(Handl ...
- Spring MVC 拦截 js,css,png 等资源
springMVC的<mvc:resources mapping="***" location="***">标签是在spring3.0.4出现的,主 ...
- Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC
内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...
- Spring MVC+Spring +Hibernate配置事务,但是事务不起作用
最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...
- Spring MVC学习初篇
Spring mvc 使用配置: <!-- 使用MVC --> <servlet> <servlet-name>defaultDispatcher</serv ...
- Spring框架下的 “接口调用、MVC请求” 调用参数、返回值、耗时信息输出
主要拦截前端或后天的请求,打印请求方法参数.返回值.耗时.异常的日志.方便开发调试,能很快定位到问题出现在哪个方法中. 前端请求拦截,mvc的拦截器 import java.util.Date; im ...
随机推荐
- POJ 3624
背包问题,在定容量的背包中放入物体求装满价值最大.因为每种物体数量只有1,故只有放与不放. #include<iostream> #include<cstring> #incl ...
- curl raise 信号出core
在使用c++多线程使用libcurl抓取网页时,遇到程序随机core掉的情况,gdb 一下出错信息有这么一条:longjmp causes uninitialized stack frame. 在网上 ...
- Android开发代码规范
目录 1.命名基本原则 2.命名基本规范 2.1编程基本命名规范 2.2分类命名规范 3.分类命名规范 3.1基本数据类型命名规范 3.2控件命名规范 3.3变量命名规范 3.4整个项目的目录规范化 ...
- Base64加密
实际开发中可能需要使用到可解密的加密方式,例如客户端记住用户的密码,客户端不能记住明文密码,那就需要对明文密码进行加密,然后在表单提交之后先对密码进行解密,在进行MD5加密和数据库中的密码进行比较实现 ...
- 枪击手机屏幕应用android源码
这款是作者最新的一款应用源码,枪击手机屏幕应用源码,该应用源码比较完整的,应用目前已经上线了一些应用商店了,大家想更深入的了解,可以到一些应用商店下载吧,直接搜索相关的关键字就可以搜到了,或者在下面有 ...
- 从零开始搭建TestCpp工程
目标: 创建一个测试工程,测试工程以列表的方式展示,没一个列表项对应一个场景 1. 创建cocos2d-x工程 现在采用脚本的方式来创建,好处是一次可以创建N个项目的工程. 首先 ...
- <Linux下echo指令>
echo这个命令我们最常见的还是在shell脚本中的使用,if语句,for语句,case语句....这些都不是对echo命令的全面了解.下面还有很多其他echo的参数: 来自本人的日常生活,和对资料查 ...
- 《postfix MAIL服务搭建(第一篇):》RHEL6
初级篇:搭建发送端.接收端.邮件别名的添加从而达到邮件群发功能的实现. 我们都知道邮件服务器是2个服务端也就是说2个端口,一个是发送的端口,一个是收邮件的端口,我们平常所使用的发送,接收邮件的过程,只 ...
- 5.servlet cookie自动登录的实例
1.要建的文档,.java用servlet创建 2.建一张登陆表格 index.jsp <%@ page language="java" import="java. ...
- sublime3 注册码
—– BEGIN LICENSE —– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 AFF6F6 ...
