首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 开源技术 >> Struts >> 正文

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

4. 完善工程

4.1 添加index画面

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

  在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>

上一页  [1] [2] [3] [4] [5] 下一页

【责编:Star】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·算法分析与设计之五大常用算法
 ·开发必备 漫谈Java加密保护
 ·嵌入式开发--ARM技术专题
 ·C/C++指针,认真了解,灵活运用
 ·.NET开发:C#实用基础教程
 ·软件测试工具QTP学习专题
 ·嵌入式开发单片机解决方案专题
 ·Java开发环境 Greenfoot 程序员手册
 ·C++对象布局及多态实现的探索
 ·常见排序算法的实现
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航