Chinaunix首页 | 论坛 | 博客
  • 博客访问: 714719
  • 博文数量: 147
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1725
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-22 10:36
文章分类

全部博文(147)

文章存档

2011年(1)

2010年(1)

2009年(35)

2008年(110)

我的朋友

分类: Java

2008-11-17 20:05:58

struts2的体系结构:
开发流程:
1.将struts2的jar文件添加到lib下面
2.将struts.properties和struts.xml文件添加到src下面,
并将struts.action.extension=action
改为
struts.action.extension=do
这两个文件下载:
文件: struts.zip
大小: 3KB
下载: 下载
3.添加struts启动项
 
        struts2
        org.apache.struts2.dispatcher.FilterDispatcher
   


   
        struts2
        /*
   

4.配置日志支持组件
  将下面的jar文件拷贝到lib目录下面
文件: log4j.zip
大小: 351KB
下载: 下载
                                                                                   5.编写jsp页面
6.配置处理器映射和处理器,有个例子struts.xml:                           
 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "">

<struts>
    <!-- Add packages here -->
    <package name="main" extends="struts-default">
        <action name="index">
            <result>login.jsp</result>
        </action>
        <action name="welcome">
            <result>welcome.jsp</result>
        </action>
        <action name="login" class="loginAction">
            <result name="success">welcome.jsp</result>
            <result name="input">login.jsp</result>
        </action>
        <action name="logout" class="logoutAction">
            <result name="success">login.jsp</result>
        </action>
        <action name="register" class="registerAction">
            <result name="success">login.jsp</result>
            <result name="input">register.jsp</result>
        </action>
    </package>
    <package name="model" extends="struts-default">
        <interceptors>
            <!-- 定义一个包含权限控制的拦截器栈 -->
            <interceptor name="authority"
                class="com.demo.struts2.common.AuthorizationInterceptor" />

            <!-- 定义一个包含权限控制的拦截器栈 -->
            <interceptor-stack name="mydefault">
                <interceptor-ref name="defaultStack" />
                <interceptor-ref name="authority" />
            </interceptor-stack>
        </interceptors>

        <!-- 定义默认拦截器 -->
        <default-interceptor-ref name="mydefault" />

        <!-- 定义全局处理结果 -->
        <global-results>
            <result name="login">login.jsp</result>
        </global-results>

        <action name="address*" class="addressAction">
            <result name="list">address.jsp</result>
            <result name="add">address_add.jsp</result>
            <result name="edit">address_edit.jsp</result>
            <result name="input">address_{1}.jsp</result>
        </action>
        <action name="schedule*" class="scheduleAction">
            <result name="list">schedule.jsp</result>
            <result name="add">schedule_add.jsp</result>
            <result name="edit">schedule_edit.jsp</result>
            <result name="input">schedule_{1}.jsp</result>
        </action>
        <action name="worklog*" class="worklogAction">
            <result name="list">worklog.jsp</result>
            <result name="add">worklog_add.jsp</result>
            <result name="edit">worklog_edit.jsp</result>
            <result name="input">worklog_{1}.jsp</result>
        </action>
        <action name="sms*" class="smsAction">
            <result name="list">sms.jsp</result>
            <result name="add">sms_add.jsp</result>
            <result name="input">sms_{1}.jsp</result>
        </action>
        <action name="notice*" class="noticeAction">
            <result name="list">notice.jsp</result>
            <result name="add">notice_add.jsp</result>
            <result name="edit">notice_edit.jsp</result>
            <result name="input">notice_{1}.jsp</result>
        </action>
        <action name="meeting*" class="meetingAction">
            <result name="list">meeting.jsp</result>
            <result name="add">meeting_add.jsp</result>
            <result name="edit">meeting_edit.jsp</result>
            <result name="input">meeting_{1}.jsp</result>
        </action>
    </package>

</struts>

7.编写处理器类

映射文件struts.xml主要使用如下的7个功能:

1.属性定义元素

 

2.导入子文件元素:

 

3.包定义元素

4.Action映射配置元素

  配置直接转发请求:

<action name="index">
   <result>login.jsp</result>
</action>

 
  旧的方法-指定处理的Action类
 

<action name="login" class="loginAction">
            <result name="success">welcome.jsp</result>
            <result name="input">login.jsp</result>
</action>
通过默认的execute()来处理。

  新的方法-指定Action的处理方法

<action name="login" class="loginAction" method=“login”>
            <result name="success">welcome.jsp</result>
            <result name="input">login.jsp</result>
 </action>
 <action name="register" class="loginAction" method=“logout”>
           <result name="success">login.jsp</result>
           <result name="input">error.jsp</result>
 </action>
通过指定的方法来处理

  通过请求地址来指定执行的函数

<action name="login" class="loginAction">
            <result name="success">welcome.jsp</result>
            <result name="input">login.jsp</result>
</action>

通过

通过

通过通配符映射

<action name="login*" class="LoginAction">
            <result name="success">welcome.jsp</result>
            <result name="input">{1}.jsp</result>
</action>
通过

成功转到success.jsp    失败转到login.jsp

通过

成功转到success.jsp    失败转到srror.jsp

5.Result配置元素

 ActionSupport中定义了几个默认的Result Token

String SUCCESS="success"
String NONE="none"
String ERROR="error"
String INPUT="input"
String LOGIN="login"

6.配置全局映射元素

<global-results>
            <result name="login">login.jsp</result>

</global-results>

7.配置拦截器

<interceptors>
            <!-- 定义一个包含权限控制的拦截器栈 -->
            <interceptor name="authority"
                class="com.demo.struts2.common.AuthorizationInterceptor" />

            <!-- 定义一个包含权限控制的拦截器栈 -->
            <interceptor-stack name="mydefault">
                <interceptor-ref name="defaultStack" />
                <interceptor-ref name="authority" />
            </interceptor-stack>
</interceptors>



   login.jsp

 

在action中使用拦截器

<action name="login" class="LoginAction">
            <result name="success">welcome.jsp</result>

            >
            >
</action>

大多数应用程序都有默认的拦截器栈,如:

默认的拦截器栈作用与上的每个action,对于那些不需要使用拦截器的Action定义在两外一个包中,这个包中依然使用系统默认的拦截器!


 
例如:权限检查的拦截器
 

public class AuthorizationInterceptor extends AbstractInterceptor{
public String interceptor(ActionInvocation invocation)throws Exception{
Map session=invocation.getInvocationContext().getSession();
String username=(String)session.get(Constants.USERNAME_KEY);
if(null!=username&&!username.equals("")){
return invocation.invoke();
}else{
System.out.println("拦截器:用户未登陆");
return Action.LOGIN;
}
}
}

Action业务控制器

1.Action可以是任意的POJO

public class LoginAction{
public String execute() throws Exception{
return "success";
}
}

2.取得页面表单数据

pubic class LoginAction{
private String username;
private Srting password;
public String getUsername(){
...
}
......

public String execute() throws Exception{
return "success";
}

}

表单的校验

1.继承ActionSupport类来编写校验函数valudate()

pubic void validate(){
if(username==null||username.equals("")){
addFieldError("username",getText("login.error.username"));
}
if(password==null||password.equals("")){
addFiledError("password",getText("login.error.password"));
}
}

2.使用XWork的xml校验规则文件

和struts1有些类似

struts标签

1.读取资源文件进行国际化显示

建立中文资源文件后要转化

${JAVAHOME}\bin\native2ascii -encoding gb2312 Application_temp.peroperties Application_zh_CN.properties

修改struts.properties中的struts.constom.il8n.resource=com.demo.struts2.ApplicationResources(没有.properties)
使用标签输出资源文件
 
2.使用表单标签进行表单校验和信息维持
3.输出错误信息的标签
 
  
  另外对于 对应的表单错误可以使用
 
  username
4.struts2的主题和模板
  默认的主题:Simple, xhtml(默认), css xhtml, archive, ajax
  可以修改主题和模板,将修改好的模板文件放在src/template/Simple下面
5.使用Sitemesh进行站点布局
  工作原理:一个请求到服务器后,如果该请求需要被Sitemesh装饰,服务器会先解释被请求的资源,然后根据配置文件获得用于该请求的装饰器,最后用装饰器装饰被请求资源,将结果返回客户端!
使用Sitemesh:
a.将sitemesh-2.3.jar拷贝到lib目录下.tld文件考到wen-inf下面
文件: sitemesh.zip
大小: 152KB
下载: 下载
b.在web.xml文件中配置sitemesh框架
 

<filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>
            com.opensymphony.module.sitemesh.filter.PageFilter
        </filter-class>
</filter>
<filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>


<taglib>
      <taglib-uri>sitemesh-page</taglib-uri>
      <taglib-location>
                /WEB-INF/sitemesh-page.tld
      </taglib-location>
</taglib>

<taglib>
      <taglib-uri>sitemesh-decorator</taglib-uri>
      <taglib-location>
                /WEB-INF/sitemesh-decorator.tld
      </taglib-location>
</taglib>

c.建立装饰器描述文件decorators.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<decorators defaultdir="/decorators">

    <decorator name="frame" page="frame.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

d.建立装饰器页面

例子:

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="com.demo.struts2.util.Constants"%>
<%@ taglib uri=""
    prefix="decorator"%>
<html>
<head>
<title>Demo - <decorator:title default="Welcome!" /></title>
<link rel="stylesheet" type="text/css" href="styles.css">
<decorator:head />
</head>

<body>
<div align="center"><%if (session.getAttribute(Constants.USERNAME_KEY) != null) {%>
<table width="100%">
    <tr>
        <td><img src='images/'></td>
        <td><img src='images/' height="90"></td>
    </tr>
    <tr>
        <td colspan="2">
        <hr>
        </td>
    </tr>
    <tr>
        <td width="160" bgcolor="#EEEEEE" valign="top">
        <table width="100%">
            <tr>
                <td>
                <li><a href="welcome.do"><s:text name="frame.main" /></a></li>
                </td>
            </tr>
            <tr>
                <td>
                <li><a href="address!init.do"><s:text name="frame.address" /></a></li>
                </td>
            </tr>
            <tr>
                <td>
                <li><a href="schedule!init.do"><s:text name="frame.schedule" /></a></li>
                </td>
            </tr>
            <tr>
                <td>
                <li><a href="worklog!init.do"><s:text name="frame.worklog" /></a></li>
                </td>
            </tr>
            <tr>
                <td>
                <li><a href="sms!init.do"><s:text name="frame.sms" /></a></li>
                </td>
            </tr>
            <tr>
                <td>
                <li><a href="notice!init.do"><s:text name="frame.notice" /></a></li>
                </td>
            </tr>
            <tr>
                <td>
                <li><a href="meeting!init.do"><s:text name="frame.meeting" /></a></li>
                </td>
            </tr>
            <tr>
                <td>
                <li><a href="logout.do"><s:text name="frame.logout" /></a></li>
                </td>
            </tr>
        </table>
        </td>
        <td align="left" valign="top"><decorator:body /></td>
    </tr>
</table>
<%} else {%> <decorator:body /> <%}%>
<hr>
2007copyright abc@163.com</div>
</body>
</html>

struts2+sring+hibernate实例在我的邮箱里tian_bian_fei@163.com

 

阅读(2967) | 评论(0) | 转发(0) |
0

上一篇:spring核心技术

下一篇:JUnit单元测试

给主人留下些什么吧!~~