当前位置:首页 > 代码 > 正文

struts源代码(struts项目)

admin 发布:2022-12-19 16:02 126


今天给各位分享struts源代码的知识,其中也会对struts项目进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

我想研究一下struts2源码,该怎么做?

我前几天也跟你有一样的想法,研究了一下struts2的源码。

建一个空白的web工程(myeclipse),正确引入struts2的jar。做个最简单的例子跑通就行。

struts2的源码主要是struts2-core-2.xxx.jar和xwork-core-2.xxxx.jar 将这两个jar与源码包关联就可以了。这样可以看到源码了。

而且如果你想测试某个类,可以自己在自己的工程了建于struts2同名的包复制源码,加上自己的测试代码,跑起来会引用你写的,而其他的类则会去struts2的包类引用。

开始有点乱,看了三四天才整出点头绪,祝你好运!

Struts的资源文件时如何初始化的--struts源码学习 - 轮上飞 - BlogJa...

[2]系统是如何启动和初始化struts的在Web.xml中有这么一段代码: action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 3 detail 3 0 startThread com.cgogo.ypindex.StartThread startParam 2 1 action *.do 在web.xml中会配置struts的中央控制器类。Web.xml配置文件的初始化是由容器来实现载入和初始化的。如果你使用的是tomcat的话,那么就是由tomcat在启动的时候会载入此web.xml文件,也就为此web应用创建了一个web Context,此context也就是你Web应用的访问入口。载入中央控制器org.apache.struts.action.ActionServlet,这是一个Servlet,那么,其就要进行servlet的初始化操作。此action是如何对Struts系统进行初始化的呢?[3]init()--ActionServlet如何初始化Struts系统?看一下ActionServlet的源代码:我们知道,在一个Servlet中,在其启动的时候,首先要执行init()方法,那么我们先来看一下actionServlet的init()方法的源代码: /** * Initialize this servlet. Most of the processing has been factored into * support methods so that you can override particular functionality at a * fairly granular level. * * @exception ServletException if we cannot configure ourselves correctly */ public void init() throws ServletException { // Wraps the entire initialization in a try/catch to better handle // unexpected exceptions and errors to provide better feedback // to the developer try {(1) initInternal();(2) initOther();(3) initServlet(); getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);(4) initModuleConfigFactory(); // Initialize modules as needed ModuleConfig moduleConfig = initModuleConfig("", config);(5) initModuleMessageResources(moduleConfig);(6) initModuleDataSources(moduleConfig);(7) initModulePlugIns(moduleConfig); moduleConfig.freeze();(8) 初始化配置参数 Enumeration names = getServletConfig().getInitParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); if (!name.startsWith("config/")) { continue; } String prefix = name.substring(6); moduleConfig = initModuleConfig (prefix, getServletConfig().getInitParameter(name)); initModuleMessageResources(moduleConfig); initModuleDataSources(moduleConfig); initModulePlugIns(moduleConfig); moduleConfig.freeze(); } this.initModulePrefixes(this.getServletContext()); this.destroyConfigDigester(); } catch (UnavailableException ex) { throw ex; } catch (Throwable t) { // The follow error message is not retrieved from internal message // resources as they may not have been able to have been // initialized log.error("Unable to initialize Struts ActionServlet due to an " + "unexpected exception or error thrown, so marking the " + "servlet as unavailable. Most likely, this is due to an " + "incorrect or missing library dependency.", t); throw new UnavailableException(t.getMessage()); } } 先来看一下初始化(1)的部分initInternal()。 [4] initInternal()--内部资源文件的初始化initInternal()就是实现内部资源文件的初始化的,也就是转为Struts系统本身提供的以下错误信息提示等文字的国际化实现的。我们看一下源代码是如何实现的: /** *InitializeourinternalMessageResourcesbundle. * *@exceptionServletExceptionifwecannotinitializetheseresources */ protectedvoidinitInternal() throwsServletException { // :FIXME: Document UnavailableException try{ internal= MessageResources.getMessageResources(internalName); } catch(MissingResourceException e) { log.error("Cannot load internal resources from ‘"+ internalName+ "‘", e); thrownewUnavailableException ("Cannot load internal resources from ‘"+ internalName+ "‘"); } }Struts根据配置文件的名字得到一个资源文件。internalName就是内部资源文件的名称,其在actionServlet中定义: /** *TheJavabasenameofourinternalresources. *@sinceStruts1.1 */ protectedString internalName= "org.apache.struts.action.ActionResources";取得后的对象是一个MessageResources的对象,保存在internal中, /** * The resources object for our internal resources. */ protected MessageResources internal = null;经过此初始化后,internal就不在是null了。至此就实现完成了内部资源文件的初始化。 /** * Initialize other global characteristics of the controller servlet. * * @exception ServletException if we cannot initialize these resources */ protected void initOther() throws ServletException { String value = null; value = getServletConfig().getInitParameter("config"); if (value != null) { config = value; } // Backwards compatibility for form beans of Java wrapper classes // Set to true for strict Struts 1.0 compatibility value = getServletConfig().getInitParameter("convertNull"); if ("true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value) || "y".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value)) { convertNull = true; } if (convertNull) { ConvertUtils.deregister(); ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class); ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class); ConvertUtils.register(new BooleanConverter(null), Boolean.class); ConvertUtils.register(new ByteConverter(null), Byte.class); ConvertUtils.register(new CharacterConverter(null), Character.class); ConvertUtils.register(new DoubleConverter(null), Double.class); ConvertUtils.register(new FloatConverter(null), Float.class); ConvertUtils.register(new IntegerConverter(null), Integer.class); ConvertUtils.register(new LongConverter(null), Long.class); ConvertUtils.register(new ShortConverter(null), Short.class); } }然后就执行(3)initServlet(); [6] initServlet()--如何初始化servlet这个初始化主要是初始化servlet的,哪些servlet呢? /** * Initialize the servlet mapping under which our controller servlet * is being accessed. This will be used in the html:form * tag to generate correct destination URLs for form submissions. * * @throws ServletException if error happens while scanning web.xml */ protected void initServlet() throws ServletException { // Remember our servlet name //这里保存当前的servlet名字,保存在actionServlet的servletName属性中 this.servletName = getServletConfig().getServletName(); // Prepare a Digester to scan the web application deployment descriptor Digester digester = new Digester(); digester.push(this); digester.setNamespaceAware(true); digester.setValidating(false); // Register our local copy of the DTDs that we can find for (int i = 0; i registrations.length; i += 2) { URL url = this.getClass().getResource(registrations[i+1]); if (url != null) { digester.register(registrations[i], url.toString()); } } // Configure the processing rules that we need digester.addCallMethod("web-app/servlet-mapping", "addServletMapping", 2); digester.addCallParam("web-app/servlet-mapping/servlet-name", 0); digester.addCallParam("web-app/servlet-mapping/url-pattern", 1); // Process the web application deployment descriptor if (log.isDebugEnabled()) { log.debug("Scanning web.xml for controller servlet mapping"); } //取得当前的配置文件 InputStream input = getServletContext().getResourceAsStream("/WEB-INF/web.xml"); if (input == null) { log.error(internal.getMessage("configWebXml")); throw new ServletException(internal.getMessage("configWebXml")); } try { //解析当前配置文件digester.parse(input); } catch (IOException e) { log.error(internal.getMessage("configWebXml"), e); throw new ServletException(e); } catch (SAXException e) { log.error(internal.getMessage("configWebXml"), e); throw new ServletException(e); } finally { try { //

怎样将struts2的源码关联到myeclipse中

另外一种方法: 1、 下载Struts的开源代码,放在相应的目录; (例如:struts-2.0.14-src, 放在G:\Study\struts\src下面)。 2、 在 Eclipse的 项目-属性-java构建路径-库中,选择一个需要关联的jar文件,选择"源代码关联项";将其关联到相应的目录;(例如 struts2-core-2.0.9.jar关联到G:\Study\struts\src\struts-2.0.14-src\src\core \src\main\java中。注意:下载的目录为 struts-2.0.14-src,关联的目录为struts-2.0.14-src\src\core\src\main\java) 不过这里没说明,要关联文件夹的话,你要选择第三个按钮,external folder

在Eclipse中,怎么查看某个类的源代码

1、首先打开eclipse,建立项目:Test,将struts2相关jar包导入到其中。在Package Explorer标签栏下操作。

2、这里查阅struts2中,struts2-core-2.3.12.jar下的源代码。在Test项目下,找到 Web App Libraries -- struts2-core-2.3.12.jar,右键单击struts2-core-2.3.12.jar,选择"Properties" 。

3、在新弹出的对话框中,选择Java  Source Attachment -- External location -- External Floder...。

4、此时,又有新的对话框弹出。找到步骤1中提到的struts2-core-2.3.12.jar的源代码在硬盘中的位置,然后单击“确定”,回到上对话框,确定信息无误后,点击“OK”

5、此时,源代码就成功和Eclipse建立连接了。

struts2源代码怎么导入到eclipse中

1.框架搭建1.1将struts2中的jar文件导入到项目中commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,freemarker-2.3.15.jar,ognl-2.7.3.jarstruts2-core-2.1.8.1.jar,xwork-core-2.1.6.jar1.2将struts.xml文件拷贝到项目的src目录下1.3修改web.xml文件添加:struts2org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterstruts2/*2.action中方法的调用方式2.1自动方法调用(只能调用execute)2.2指定方法调用(通过设置action标签中的method属性)2.3动态方法调用(在调用时,在action后加!方法名称,如:login!deletUser)注意:2.4通配符调用3.action接收客户端参数的方式3.1直接在action中定义参数变量,并生成set和get方法3.2定义接收参数的类注意:都要为action的成员变量提供get和set方法3.3让action实现ModelDriven接口,并实现里面的getModel方法4.获取request,session,application的方式4.1用ActionContext获取,实际上获取到的都是Map对象4.2用ServletActionContext获取,获取到的是基于ServletAPI的对象4.3让action实现RequestAware,SessionAware,ApplicationAware接口,并实现里面的方法5.四种转向5.1action转发到页面(默认)5.2action重定向到页面5.3action转发到actionlogin/loginlogin5.4action重定向到actionlogin

struts源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于struts项目、struts源代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/11787.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载