您的位置:首页 > 编程语言 > Java开发

Struts2的一些简单配置,供同同们参考

2012-03-08 05:06 295 查看
<!--MVC的优势

1,多个视图可以对应一个模型,当模型发生改变的时候,易于维护。
2,模型返回的数据和逻辑分离,模型数据可用于任何的显示技术。
3,应用分为了M,V,C三层,提供了各层之间的耦合,提供了可扩展性。
4,C把不同的视图和模型组合在一起。
5,MVC符合工程化管理的精神。

运用struts,需要在web。xml中配置filter或者servlet,这是这个框架的核心,负责拦截所有的用户请求,具体内容如下:
<filter>
<!--定义核心filter的名称-->
<filter-name>struts2</filter-name>
<!--定义核心filter的实现类-->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->

<!--
//对于每一个action来说,都是定义的处理用户请求的Action类,属于C,都要继承ActionSupport 如:

public class LoginAction extends ActionSupport{
//只需要为每一个提供set和get方法既可以,系统自动会注入页面传过来的属性。
private String name ;
*********************;
//处理请求的方法
public String execute()throws Exception{
//中间的逻辑大同小异
*********************;
/*Action访问Servlet API ,即Servlet中的session,application,cookie等如何得到即应用?
Action通过ActionContext类,来访问Servlet API,
ServletContextAware,实现这个接口的action可以方法web的ServletContext
ServletRequestAware,实现这个接口的action可以访问用户的request;
ServletResponseAware,实现这个接口的action可以访问服务器响应的response;
ActionContext的几个常用方法是:
Object get(Object key),类似于request中的getAttribute(String name);
Map getApplication(),返回一个map,该对象模拟了ServletContext
类似于此的函数还有很多,可以自行查看文档,例子如下:
*/
ActionContext ac=ActionContext.getContext();
Integer counter=(Integer)ac.getApplication.get("counter");
ac.getSession().put("counter",counter);//这样即可得到session
//至于jsp的页面是${sessionScope.counter}来得到放入其中的键值的。

//最后的返回值
return SUCCESS;
/*为什么可以直接返回SUCCESS而不会出错了??
这是因为在Action这个接口中,有关于SUCCESS的定义,而ActionSupport实现了Action这个接口内容如下:
public static final String SUCCESS="success";
public static final String INPUT="input";
等等,所以返回的SUCCESS,其实返回的是字符串success那么既可以和下面的视图相对于啦。
*/
}
}
-->
<? xml version="1.0" encoding="GBK" ?>
<! DOTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0 //EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!--struts的处理流程:
1,浏览器发送请求
2,FilterDispacher拦截请求,并调用对应的Action中的方法进行处理
3,Action调用Model组件来实现
4,Model返回业务结果
5,Action根据处理结果方法逻辑视图名称
6,FilterDispacher根据逻辑视图找到对应的物理视图,并forward到其中
7,物理视图生成响应内容,然后返回给FilterDispacher
8,FilterDispacher将结果输出给用户。
-->
<struts>
<!--关于一个真正的完全struts文件,应该有的属性有很多,这个也要查文档-->

<!--通过include来导入其他的文件配置-->
<include file="struts-part.xml">

<!--关于struts中的一些常量配置,需要查看相关文档啦!!不过struts提供了三种管理常量的方法,分别是,struts.properties,通过struts中的constant属性,还可以在web.xml中配置。-->
<!--指定全局国际化资源文件pass名-->
<constant name="struts.custom.i18n.resources" value="messageResource" />
<!--指定全局国际化资源使用的字符集-->
<constant name="struts.i18n.encoding" value="GBK">

<!--为上面的LoginAction注册,配置Action是为了指定那个请求对于那个Action进行处理,-->
<!--包和命名空间,包有name:指定包的名称,可以被其他包引用的key、extends:继承其他包、namespace:指定包的命名空间、abstract:指定包是否是一个抽象包,抽象包中不能有Action-->
<package name="skill" extends="default" namespace="/skill" abstract="true"></package>
<package name="default" extends="struts-default">

<!--Action的基本配置 name指定action名称,calss指定实现的类,method指定实现的方法,所以一个实现类可以对应多个action-->
<!--假如请求中没有包含对方法的指定,那该怎么办??不用担心,默认就会是execute方法-->
<action name="login" class="LoginAction" method="execute">
<!--result是逻辑视图与物理资源之间的一种映射-->
<result name="input">/login.jsp</result>
<result name="success">/welcome.jsp</result>

<!--配置结果 name属性默认为success type为结果类型,默认是dispatcher与jsp整合的类型,-->
<result name="success" type="">/welcome.jsp</result>
<!--复杂的result可以使用多种视图技术如JSP Velocity FreeMarker,具体可以根据需要配置。-->
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
</result-types>
</action>

<!--通配符的使用!其中{1}代表对前面的*的内容,这样当有多个action时候,就可以沈略配置信息啦!-->
<action name="*action" class="ServiceAction" method="{1}">
************************************************
</action>

<package>
</struts>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息