在加载完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>
添加方法跟添加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方法执行完毕后,把客户请求在转发给相应的页面。
添加方法跟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"));
}
}
}
与添加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>
·Java Reflection (JAVA反射)详解 (7796次浏览)
·Java: JNI完全手册 (6280次浏览)
·Java程序员面试陷阱大全 (3448次浏览)
·Java连接数据库谈 (3419次浏览)
·JDBC连接数据库经验集萃 (3287次浏览)
·Struts教程-Struts模块化编程教程 (3179次浏览)
·图解Java开发工具JBuilder 9.0 (3022次浏览)
·Eclipse插件之Spring IDE (2694次浏览)
·jdk5.0 tomcat5.0配置全攻略 (1626次浏览)
·AOP概念 10-31
·Acegi框架介绍 10-31
·5分钟内在Fedora Linux上安装JDK6/Java SE 6 10-31
·2006年Java开发技术回顾与展望 10-31
·2006-2007年度JAVA平台开发工具的应用状况 10-31
·2006-2007年度JAVA开发与应用情况研究 10-31
·“三步走”成为一名优秀的程序员 10-31
·.net平台下nhibernate配置 10-31
·BD影碟机确定采Java技术 10-28



