struts配置及简单登陆程序演示

struts给人们提供了一个程序设计的框架,它从理论上把程序为M(模块,也就是数据处理部分),V(视图/交互界面),C(控制,也就是程序流转)。系统地学习struts,将能够极大地帮助我们设计出架构良好的程序,无论你的程序是B/S还是C/S架构。学习Struts可以和设计模式进行结合,这样可以加深我们对整个程序设计框架的理解,这是后话。下面的例子程序,包含了3个jsp页面:login.jsp(用户登录界面),main.jsp(登陆成功提示界面),error.jsp(登陆失败提示界面),我们可以从这个简单的例子学习基本的struts-config.xml和web.xml文件配置。

首先需要到http://jakarta.apache.org下载Tomcat,JDK和struts包,笔者使用的版本是Tomcat 5.0.30,JDK1.4.2和struts-1.2.7,别的版本应该也不会存在的差异。至于环境变量和Tomcat的虚拟目录怎么设置,笔者今天不在这里做介绍,网络上这些文章很多。

一、首先我们将下载的struts包进行解压缩,拷贝lib下的struts.jar到你的虚拟目录下的/WEB-INF/lib目录,拷贝struts-bean.tld,struts-html.tld,struts-logic.tld,struts-nested.tld,struts-tiles.tld到你的虚拟目录下的/WEB-INF下,这是必须要做的准备工作。

二、然后在/WEB-INF下面创建struts-config.xml和web.xml文件(当然你也可以从struts解压文件中拷贝过来进行修改)。

三、struts-config.xml文件配置如下:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- ====================数据源定义 -->
<data-sources/>
<!-- =================== Form Bean表单定义 -->
<form-beans>
<form-bean name="loginForm" type="org.apache.my.form.LoginForm">
<form-property name="username" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
</form-bean>
</form-beans>
<!-- =====================全局异常定义 -->
<global-exceptions/>
<!-- ===================== 全局流转定义 -->
<global-forwards/>
<!-- =====================Action Mapping 定义 -->
<action-mappings>
<action path="/login" type="org.apache.my.action.LoginAction"
name="loginForm" scope="request" input="/login.jsp">
<forward name="success" path="/main.jsp" />
<forward name="failed" path="/error.jsp" />
</action>
</action-mappings>

<!-- =====================资源文件定义 -->
<message-resources parameter="org.apache.my.ApplicationResources" />
</struts-config>

配置说明:数据源根据自己程序的需要添加,如果没有,保留如上的空标签即可。全局异常定义和全局流转定义原则和数据源一致。
Form bean定义中,标签<form-bean name="loginForm" type="org.apache.my.form.LoginForm">中的name属于我们任意定义,但尽量能表达清楚大致意义,type="org.apache.my.form.LoginForm"表示我们在/WEB-INF/classes/org/apache.my.form目录下有一个LoginForm.class文件,一个jsp文件都对应一对<form-bean></form-bean>标签。jsp文件中对应的输入框对应一个<form-property/>标签,其中name="username"必须和jsp文件中的输入框的名称保持一致,type="java.lang.String"表示该输入框接受的是字符串。
在Action Mapping 定义部分,一对<action></action>对应jsp页面的一个action动作。参数path="/login"表示调用的路径,type="org.apache.my.action.LoginAction表示响应这个action动作的class文件,name="loginForm"和<form-bean>标签中的定义的一致,表示响应页面对应的form bean,scope="request"表示jsp参数作用范围,input="/login.jsp"表示form bean对应的输入jsp文件。
<message-resources/>对应我们所使用的资源文件的位置。根据需要决定是否保留。

注意:<?xml version="1.0" encoding="ISO-8859-1" ?>的编码格式不可以随便定义,可能导致tomcat启动出现严重错误。

四、web.xml文件配置
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</web-app>

配置说明:config参数指定了struts-config.xml的位置。action指定了程序处理的文件后缀类型,你也可以定义为.cool。
<taglib-uri>/tags/struts-bean</taglib-uri>表示将来可以在jsp文件中引用的虚拟路径,<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>表示了虚拟路径对应的实际文件路径。

五、程序中相应的代码列表
--------------login.jsp文件--------------------
<%@ page language="java" contentType="text/html; charset=GB18030"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<html:html>
<head>
<title> Struts 主页</title>
</head>
<body>
<h3>正确的登录用户名和密码是:hh</h3>
<P>
<html:form action="login" focus="username">
<table>
<tr>
<td>用户名:</td>
<td><html:text property="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><html:password property="password"/></td>
</tr>
<tr>
<td colspan=2 align=right>
<html:submit value="确定 "/>
</td>
</tr>
</table>
</html:form>
</P>
</body>
</html:html>

--------------main.jsp文件--------------------------
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<html:html locale="true">
<head>
<title>成功了</title>
<html:base/>
</head>
<body>
<P>注册成功!</P>
<P><BR>
<BR>
<A href="login.jsp">再试一次</A>?
</P>
</body>
</html:html>

--------------error.jsp文件---------------------------
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html:html>
<head>
<title>出错了</title>
<html:base/>
</head>
<body>
<P>登陆失败!请检查用户名和密码是否正确!</P>
<P><BR>
<BR>
<A href="login.jsp">再试一次</A>?
</P>
</body>
</html:html>
--------------LoginForm.java文件--------------------
package org.apache.my.form;
import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm {
public String username="";
public String password="";

public LoginForm(){}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

--------------LoginAction.java文件---------------------
package org.apache.my.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.my.form.LoginForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public final class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

String userName = ((LoginForm) form).getUsername();
String password = ((LoginForm) form).getPassword();
System.out.println("用户名:"+userName);
System.out.println("密码:"+password);
if (userName.equals("hh") && password.equals("hh")) {
HttpSession session = request.getSession();
session.setAttribute("loggedIn", "1");
return mapping.findForward("success");
}
else {
return mapping.findForward("failed");
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值