说在前面的话
个人研究struts源码有很长时间了,虽然目前很多公司都开始在用spring mvc,但是个人觉得那毕竟是工具,重要的是能够找到学习源码的方法及出路,真正掌握每个框架的精髓,才能够在软件行业有一席之位。希望我能够带着读者一步一步进入状态,当然,也需要读者自己的坚持,能够掌握struts框架的设计哲学,对你成为架构师,项目经理以后能够开发一套更优秀的框架带来更完美的设计思路。
搭建一个简单案例
在这里个人还是带着读者手工搭建一个简单的Hello World级别的案例帮读者来回顾struts2的最基础的知识(个人认为是有必要的,当然如果自己够优秀,可以跳过这节),也可以方便下面进行的源码分析做准备,首先将如下包放置到lib目录下,如图:
编写一个简单的LoginAction.java文件
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
private static final long serialVersionUID = -74906200993380354L;
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String execute() throws Exception
{
return SUCCESS;
}
}
编写登录页面login.jsp源码
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:form action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
编写返回result.jsp源码
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
username: ${requestScope.username }<br>
password: ${requestScope.password }
</body>
</html>
配置部署描述符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>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>
</web-app>
编写Strut.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="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success">/result.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
注意:将该文件放置到src目录下即可
启动tomcat,访问http://localhost:8081/struts2/login.jsp(请根据读者的环境来访问)
访问会出现登录框,登录后可以看到自己输入的用户名和密码,到此一个简单的示例就完成了。
Struts启动日志分析:
启动tomcat时打印出如下信息:
2014-6-10 16:56:07 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: E:\Program Files\Java\jdk1.6.0_35\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;E:/Program Files/Java/jdk1.6.0_35/bin/../jre/bin/server;E:/Program Files/Java/jdk1.6.0_35/bin/../jre/bin;E:/Program Files/Java/jdk1.6.0_35/bin/../jre/lib/amd64;F:\oracle\sun\product\11.2.0\dbhome_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\TortoiseSVN\bin;E:\Program Files\Java\jdk1.6.0_35\bin;E:\jboss-4.2.2.GA\bin;F:\tomcat-7.0.40\bin;E:\mysql\bin;.;;E:\eclipse;;.
2014-6-10 16:56:07 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:struts2' did not find a matching property.
2014-6-10 16:56:07 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8081
2014-6-10 16:56:07 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 529 ms
2014-6-10 16:56:07 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2014-6-10 16:56:07 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.39
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-default.xml]
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to locate configuration files of the name struts-plugin.xml, skipping
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-plugin.xml]
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts.xml]
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.ObjectFactory)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.FileManagerFactory)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.XWorkConverter)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.TextProvider)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.LocaleProvider)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.ActionProxyFactory)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.ObjectTypeDeterminer)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.dispatcher.mapper.ActionMapper)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (jakarta) for (org.apache.struts2.dispatcher.multipart.MultiPartRequest)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.views.freemarker.FreemarkerManager)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.components.UrlRenderer)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.validator.ActionValidatorManager)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.ValueStackFactory)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.reflection.ReflectionProvider)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.reflection.ReflectionContextFactory)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.util.PatternMatcher)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.dispatcher.StaticContentLoader)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.UnknownHandlerManager)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (org.apache.struts2.views.util.UrlHelper)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.CollectionConverter)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.ArrayConverter)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.DateConverter)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.NumberConverter)
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Choosing bean (struts) for (com.opensymphony.xwork2.conversion.impl.StringConverter)
2014-6-10 16:56:09 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8081
2014-6-10 16:56:09 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8010
2014-6-10 16:56:09 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/20 config=null
2014-6-10 16:56:09 org.apache.catalina.startup.Catalina start
信息: Server startup in 1187 ms
当然,在这里我开始并不解释每行日志信息的详细信息(详细信息会在以后的分析中一步一步讲解到),现在我只是带着大家看启动服务器时配置文件的加载顺序。
信息: Parsing configuration file [struts-default.xml]
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to locate configuration files of the name struts-plugin.xml, skipping
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts-plugin.xml]
2014-6-10 16:56:08 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Parsing configuration file [struts.xml]
现在大家很明显能感觉到struts加载配置文件
struts-default.xml----->struts-plugin.xml----->struts.xml
该处只要读者知道该顺序即可,详细的在什么地方加载在后面分析中会有讲解。
然后请读者将该案例和struts源码关联上,本人在此不再赘述。
到目前为止,分析源码环境和准备工作已经到此为止。