Struts 二实现类似ThinkPHP的MVC开发方式

WBOY
Release: 2016-06-13 11:50:27
Original
1110 people have browsed it

Struts 2实现类似ThinkPHP的MVC开发方式

ThinkPHP是PHP的一个MVC开源框架,Struts 2是Java的MVC开源框架。虽然编程语言不同,但是他们都是解决同样的问题。

用过ThinkPHP的同学应该都知道,ThinkPHP是一个非常简单的框架,使用起来很舒服。相对来说Struts 2则没有这么简单。

本文介绍如何配置Struts 2,让它使用起来像ThinkPHP一样简单。

在介绍Struts 2之前,首先了解一下ThinkPHP的开发方式。

和Struts 2一样,ThinkPHP也需要用Action来充当控制器(controller),需要页面来充当视图(view)。

下面是一个简单的ThinkPHP工程的目录结构:


其中Action目录下就是控制器,其功能和Struts 2的Action类似。Tpl目录下是html页面,也就是视图,类似于jsp页面的功能。

再看其中一个Action的代码:

<?phpclass FirstAction extends Action {		public function show() {				/**		 * 逻辑代码...		 */				$this->display ();	}		public function add() {				/**		 * 逻辑代码...		 */				$this->display ();	}		public function delete() {				/**		 * 逻辑代码...		 */				$this->display ();	}		public function update() {				/**		 * 逻辑代码...		 */		$this->display ();	}}?>
Copy after login
此时在浏览器输入http://localhost/First/update,就可以调用FirstAction的update方法处理请求,再调用Tpl/First/update.html显示。

也就是说,如果需要加一个功能模块Abc,只需要添加一个AbcAction.class.php,其中加入操作函数def,再在Tpl/Abc文件夹下创建一个文件名和函数名称一致的def.html文件,下面就可以开发逻辑和页面了,而且直接就可以用http://localhost/Abc/def访问,非常方便。整个项目很清晰,想找的某个http地址对应的Action的函数以及html文件也非常容易。

而Struts 2由于有struts.xml配置文件,如果加入一个功能模块,除了需要加入Action类和jsp页面,还需要在struts.xml中配置这个Action对应的http地址、对应的Action中的方法、以及对应的jsp文件。而如果想通过http地址找到对应的Action中的方法和jsp文件,也需要查看struts.xml中的配置。

在Struts 2的使用中,约定大于配置是个很重要的原则。ThinkPHP的开发方式就是一个比较好的约定。下面就介绍如何用Struts 2实现类似ThinkPHP的开发方式。

首先是struts.xml文件的配置:

<?xml version="1.0" encoding="UTF-8"?><struts>	<constant name="struts.enable.SlashesInActionNames" value="true"></constant>	<package name="default" namespace="/" extends="struts-default">		<action name="/*/*" class="com.xxg.action.{1}Action" method="{2}">			<result name="success">/Tpl/{1}/{2}.jsp</result>		</action>	</package></struts>
Copy after login
实现类似ThinkPHP的开发方式,需要在struts.xml中用到Wildcards,也就是通配符。其中name="/*/*"匹配http地址。第一个*是Action的类名:class="com.xxg.action.{1}Action";第二个*是Action中方法的名称:method="{2}",另外还是jsp的文件名:/Tpl/{1}/{2}.jsp

这里设置了:

<constant name="struts.enable.SlashesInActionNames" value="true"></constant>
Copy after login
表示action的name中允许出现斜杠"/"。

下面是这个项目的目录结构:


其中一个Action的代码:

package com.xxg.action;import com.opensymphony.xwork2.ActionSupport;public class FirstAction extends ActionSupport {	public String show() {		/**		 * 逻辑代码...		 */		return SUCCESS;	}	public String add() {		/**		 * 逻辑代码...		 */		return SUCCESS;	}	public String delete() {		/**		 * 逻辑代码...		 */		return SUCCESS;	}	public String update() {		/**		 * 逻辑代码...		 */		return SUCCESS;	}}
Copy after login
如此一来,在开发的过程中,所要做的就和ThinkPHP类似,加入Action,加入对应的方法,再添加一个和方法名同名的jsp文件,直接访问地址:http://localhost/Action名称/方法名称就可以直接访问,不再需要向struts.xml文件中添加action。另外,通过http地址也很容易找到对应的Action其中的方法以及对应的jsp文件,也不再需要查询struts.xml。


作者:叉叉哥   转载请注明出处:http://blog.csdn.net/xiao__gui/article/details/21073605



2楼JPFRMD3小时前
很好的文章,下次好好看看
1楼5iasp4小时前
不错,很灵活.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!