频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

创建struts1.2 + Hibernate3.0 Web工程

来源: 作者: 出处:巧巧读书 2006-08-12 进入讨论组
关 键 词:apache  css  eclipse  hibernate  html  
上一页 1 2 3 4 5 下一页 

4. 完善工程

4.1 添加index画面

  在加载完Struts结构之后,就可以在这个工程中添加内容了。我们可以添加一个画面JSP。添加JSP很简单,点击工程的右键,添加JSP。Wizard会弹出一个添加的对话框。如下图:

创建struts1.2 + Hibernate3.0 Web工程(图五)

  在File Name栏中改变文件名称,改成index.jsp把这个jsp作为Web工程的第一个画面。

  Template To Use 栏可以选择第五项(选择选择其他项也可以)。

  对于新生成的jsp文件进行必要的修改。

Index.jsp文件:
<taglib>
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.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>
  <h3><bean:message key="index.heading"/></h3>
<html:link page="/logon.jsp"><bean:message key="index.logon"/></html:link>

</body>
</html>

 其中蓝色和橙色部分是修改的内容,蓝色部分是今后自动添加jsp文件必须修改的地方,让这个页面用到的tag都能在这个工程中定义的tag集合中找到。

  橙色部分是画面的主要显示内容。<h3></h3>的内容是一段文本,内容被struts结构的静态文本集合中的index.heading给替换掉了。这个文本的内容可以在工程的src\com\yourcompany\struts\ApplicationResources.properties文件中找到。Struts结构推荐用户将页面上的静态文本用ApplicationResources的形式替换。这样可以在大量的维护页面文字时候,感到便捷很多,同时,也大大减轻了多国语言版本网页的维护。

  <html:link page="/logon.jsp">相当于html语言中的<a herf=xxxxx>。这个tag是在/WEB-INF/struts-html.tld中可以找到并在显示页面的时候,被转义成<a herf=xxxxx>

4.2 添加logon画面

  添加方法跟添加index画面相同。修改内容稍微有些差别。

Logon.jsp的内容:

<taglib>
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
  <head>
    <html:base />
   
    <title>logon.jsp</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">
  </head>
 
  <body>
    <html:form action="/logon.do" method="post"  focus="userName">
      <table border="0">
        <tr>
          <td><bean:message key="prompt.username"/></td>
          <td><html:text property="userName" /></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><html:password property="password" /></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><html:submit /></td>
        </tr>
      </table>
    </html:form>
  </body>
</html:html>

  其中,蓝色部分是引用本工程的tag标示库,红色的部分是表单属性名称的修改,和指定action动作的名称。

4.3 修改WEB-INF/struts-config.xml

  画面上出现了form,那么根据struts的结构要求,就必须在WEB-INF/struts-config.xml中明确这个form的formbean(表单内容校验的java class)是什么。执行这个form的action(表单执行的内部逻辑)是什么,以及action的结果会产生怎样的画面迁移。这些都是在WEB-INF/struts-config.xml中定义的。也就是标准的MVC架构所要求的。
<taglib>
<struts-config>
  <form-beans >
    <form-bean name="logonForm" type="com.yourcompany.forms.
LogonForm" />
  </form-beans>


  <global-forwards >
    <forward name="logon" path="/logon.jsp" />
  </global-forwards>


  <action-mappings >
    <action
      path="/logon"
      type="com.yourcompany.actions.LogonAction"
      name="logonForm"
      scope="request"
      input="/logon.jsp">
      <forward name="success" path="/menu.jsp" />
      <forward name="fails" path="/logon.jsp" />
    </action>
  </action-mappings>

  <message-resources parameter="com.yourcompany.struts.
ApplicationResources" />
</struts-config>

  其中<action>部分是说明action的属性。

  Path 指定Action处理的URL

  Type 指定Action的类名

  Name 指定Action主力的ActionForm名,与<form-beans >元素的name属性匹配。

  Scope 指定ActionForm存在的范围

  Input 指定包含客户提交表单的网页,如果ActionForm的Validate方法返回错误,则因该把用户请求转发到这个网页。

  Validate 如果取值为true,则表示ActionServlet应该调用ActionForm的validate方法

  Forward 就是Action的execute方法执行完毕后,把客户请求在转发给相应的页面。

4.4 添加formbean和actionbean

  添加方法跟JSP相同,但是在选择superclass的时候,formbean要选择ActionForm作为类的父类。Actionbean的父类是Action

  下面是各自的代码

LogonForm.java文件内容:

<taglib>
package com.yourcompany.forms;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LogonForm extends ActionForm {

private static final long serialVersionUID
= 7322786881443789688L;
// ------------------------------
--------------------------- Instance Variables
    private String username = null;
    private String password = null;

// -------------------------
-------------------------------- Methods
    public String getUserName() {
return (this.username);
    }

    public void setUserName(String username) {
        this.username = username;
    }

    public String getPassword() {
return (this.password);
    }

    public void setPassword(String password) {
        this.password = password;
    }

public void reset(ActionMapping mapping,
HttpServletRequest request) {
this.password = null;
this.username = null;
}

}

LogonAction.java文件:
package com.yourcompany.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.yourcompany.forms.LogonForm;

public class LogonAction extends Action {

public ActionForward execute(ActionMapping
mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

String userName = null;
String password = null;

if (form != null) {
userName = ((LogonForm) form).getUserName();
password = ((LogonForm) form).getPassword();
}
if(userName.equals(“test1”) && password.
equals(“test1”)){
return (mapping.findForward("success"));
}
else{
return (mapping.findForward("fails"));
}
}

}

4.5 添加menu画面

  与添加index画面的方法相同。下面是menu.jsp文件的内容

<taglib>
<%@ page language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
  <head>
    <html:base />
        <title>menu.jsp</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">
  </head>
    <body>
  <h3><bean:message key="menu.message"/></h3>
  </body>
</html:html>

更多文章 更多内容请看Hibernate相关文章  Hibernate原理与配置  Struts框架应用专题专题,或进入讨论组讨论。
上一页 1 2 3 4 5 下一页 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
·Hibernate原理与配置 (303篇文章)
·Struts框架应用专题 (417篇文章)
·Hibernate相关文章 (303篇文章)
热点标签: apache  css  eclipse  hibernate  html  
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
百度推荐,商机无限
搜索您感兴趣的内容
 
Web 本站
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章