前言
最近学了SSH三大框架的基础部分,最近几周将陆续更新笔记到博客上,今天第一篇。。。
第一个struts2案例-HelloWorld
web.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.xml中配置的StrutsPrepareAndExecuteFilter核心过滤器
类图
MessageStore类
package com.dfdc.helloworld.model;
public class MessageStore {
private String message;
public MessageStore() {
setMessage("Hello Struts User");
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String toString() {
return message + " (from toString)";
}
}
HelloWorldAction类
package com.dfdc.helloworld.action;
import com.dfdc.helloworld.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWordAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private MessageStore messageStore;
private static int helloCount = 0;
private String userName;
public MessageStore getMessageStore() {
return messageStore;
}
public void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}
public int getHelloCount() {
return helloCount;
}
public void setHelloCount(int helloCount) {
HelloWordAction.helloCount = helloCount;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String execute() throws Exception {
messageStore = new MessageStore();
helloCount ++;
if (userName != null) {
messageStore.setMessage(userName);
}
return SUCCESS;
}
}
helloword.jsp内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!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 World!</title>
</head>
<body>
<!-- 从值栈中获取消息并打印 -->
<h2><s:property value="messageStore.message"/></h2>
<hr>
<!-- 会自动转换最常用的数据类型 (int, double, boolean)与它们的字符串等效 -->
<p>我已说 hello <s:property value="helloCount" /> 次!</p>
<hr>
<p><s:property value="messageStore" /></p>
</body>
</html>
struts.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="helloworld" namespace="/" extends="struts-default">
<!-- HelloWordAction
当前动作类访问
-->
<action name="hello" class="com.dfdc.helloworld.action.HelloWordAction" method="execute">
<result name="success">/helloworld/helloworld.jsp</result>
</action>
</package>
</struts>
代码如何工作
在浏览器输入http://localhost:8080/Struts2/helloworld/hello.action(注:Struts2为部署在Tomcat中的的项目名称)的URL请求后,
服务器做了如下工作:
- 容器接收到了Web服务器对资源hello.action的请求,根据web.xml中的配置,
服务器将包含有.action后缀的请求转到
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类进行处理。
这个StrutsPrepareAndExecuteFilter是框架的一个进入点; - 框架在struts.xml配置文件中找到名为HelloWordAction的action对应的类。框架初始化该Action并且执行该Action类的execute方法;
- execute方法将信息放入message变量中,并返回SUCCESS。框架检查配置以查看当返回SUCCESS时对应的页面。
框架告诉容器来获得请求返回的结果页面helloworld.jsp; - 在helloworld.jsp执行完后,标签调用HelloWorldAction类中的getMessage方法来获得message的值,并将合并了值的页面呈现给用户浏览器;
struts2处理流程图
struts2的MVC架构模式
控制器(Controller)- StrutsPrepareAndExecuteFilter
- J2EE应用中,Controller 可能是一个servlet,现在一般用Struts2/Spring Framework实现。
- 即web.xml配置文件的StrutsPrepareAndExecuteFilter核心过滤器。
- 用户请求首先到达前端控制器StrutsPrepareAndExecuteFilter。StrutsPrepareAndExecuteFilter负责根据用户提交的URL和struts.xml中的配置,来选择合适的动作(Action),让这个Action来处理用户的请求。
- StrutsPrepareAndExecuteFilter其实是一个过滤器(Filter,servlet规范中的一种web组件),它是Struts2核心包里已经做好的类,不需要我们去开发,只是要在项目的web.xml中配置一下即可。StrutsPrepareAndExecuteFilter体现了J2EE核心设计模式中的前端控制器模式。
作用
用来控制应用程序的流程和处理视图所发出的请求。
当控制器接收到用户的请求后,会将用户的数据和模型的更新相映射,也就是调用模型来实现用户请求的功能;然后控制器会选择用于响应的视图,把模型更新后的数据展示给用户。
模型(Model)
- Model 则是由一个实体Bean或Action动作类来实现。
- 即上面写的HelloWorldAction类。
- 在用户请求经过StrutsPrepareAndExecuteFilter之后,被分发到了合适的动作Action对象。Action负责把用户请求中的参数组装成合适的数据模型,并调用相应的业务逻辑进行真正的功能处理,获取下一个视图展示所需要的数据。
- Struts2 的Action,相比于别的web框架的动作处理,它实现了与Servlet API的解耦,使得Action里面不需要再直接去引用和使用HttpServletRequest与HttpServletResponse等接口。 因而使得Action的单元测试更加简单,而且强大的类型转换也使得我们少做了很多重复的工作。
作用
负责封装应用的状态,并实现应用的功能。
通常分为数据模型和业务逻辑模型,数据模型用来存放业务数据,比如订单信息、用户信息等;而业务逻辑模型包含应用的业务操作,比如订单的添加或者修改等。
视图(View)
- 在J2EE应用程序中,视图(View)可能由Java Server Page(JSP)承担。生成 View 的代码则可能是一个servlet的一部分,特别是在客户端服务端交互的时候。
- 即上面的helloworld.jsp。
- 视图结果用来把动作中获取到的数据展现给用户。在Struts2中有多种优秀的结果展示方式,常规的jsp,模板 freemarker、velocity,还有各种其它专业的展示方式,如图表jfreechart、报表JasperReports、将XML转化为 HTML的XSLT等等。而且各种视图结果在同一个工程里面可以混合出现。
作用
用来将模型的内容展现给用户,用户可以通过视图来请求模型进行更新。
视图从模型获得要展示的数据,然后用自己的方式展现给用户,相当于提供界面来与用户进行人机交互;用户在界面上操作或者填写完成后,会点击提交按钮或是以其它触发事件的方式,来向控制器发出请求。
以上。。。
MVC部分内容参考:
http://www.cnblogs.com/lyp3314/archive/2013/01/24/2875853.html