您的位置:首页 > 编程语言 > Java开发

第一个Struts2的实例:

2008-10-22 00:35 316 查看
第一个Struts2的实例:
本实例的开发工具及语言:eclipse 3.2 + struts2 + tomcat 5.5
实现用户登陆功能,在这因没有连接数据库,所以用固定的用户名和密码登陆.

用户名:luanmad
密码:admin

本实例用到的文件及其结构:(看图1)
//***********************************************************************
K:/ECLIPSEWORKS/MYFIRSTSTRUTS2
│ .classpath (ECLIPSE 自动生成)
│ .mymetadata (ECLIPSE 自动生成)
│ .project (ECLIPSE 自动生成)

├─.myeclipse (ECLIPSE 自动生成)
├─src
│ │ struts.xml
│ │
│ └─cn
│ └─struts2
│ LoginAction.java

└─WebRoot
│ login.jsp
│ loginFailure.jsp
│ loginSuccess.jsp

├─META-INF
│ MANIFEST.MF

└─WEB-INF
│ web.xml

├─classes
│ │ struts.xml
│ │
│ └─cn
│ └─struts2
│ LoginAction.class

└─lib
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.2.jar
xwork-2.0.5.jar

//***********************************************************************

1.Tomcat 5.5的具体配置在这里就赘述了,可以上网搜(一大把的)
本实例配置用到的TOMCAT 5.5 文件是server.xml.

在你安装好的TOMCAT 5.5目录下找到conf的文件夹,下面有一个叫server.xml的文件(如:C:/tomcat 5.5/conf/server.xml),打开此文件进行编辑,在<Host>和</Host>之间加入一句:
<Context path="/MyFirstStruts2" docBase="K:/EclipsWorks/MyFirstStruts2/WebRoot" debug="0" reloadable="true" crossContext="true">
</Context>

此句是用于让TOMCAT 5.5 找到你的启动程序;

2.编写LoginAction.java 文件,用于处理登陆的作息,在(MyFirstStruts2/src/cn/struts2/LoginAction.java)
LoginAction.java
//***********************************************************************
package cn.struts2;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
private String name;
private String password;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}

public String execute()throws Exception
{
String path=null;
if(name.equals("luanmad") && password.equals("admin"))
{
path = SUCCESS;
}
else
{
path = ERROR;
}
return path;
}

//重写一个validate()函数,它用来对用户输入信息的验证,它先于execute()函数执行
@Override
public void validate()
{
// 验证用户名和密码是否为空
if(null == name || "".equals(name))
{
this.addFieldError("name", "name is required!");
}

if(null == password || "".equals(password))
{
this.addFieldError("password", "password is required!");
}

}

}

//***********************************************************************

3.配置struts.xml文件(src/struts.xml)
struts.xml
//***********************************************************************

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="struts2" extends="struts-default">
<action name="login" class="cn.struts2.LoginAction">
<result name="success">/loginSuccess.jsp</result>
<result name="error">/loginFailure.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>

</struts>
//***********************************************************************

4.配置web.xml文件(WebRoot/WEB-INF/web.xml)
web.xml
//***********************************************************************

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

//***********************************************************************

5.三个.jsp文件(在WebRoot文件夹下)

login.jsp
//***********************************************************************

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login2.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>
<s:form action="login" method="post">
<s:textfield name="name" label="User Name:"></s:textfield>
<s:password name="password" label="Password"></s:password>
<s:submit label="submit"></s:submit>
<s:reset label="reset"></s:reset>
</s:form>
</body>
</html>

//***********************************************************************

loginSuccess.jsp
//***********************************************************************

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login2.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>
<h1>登陆成功!</h1>
<h2>
User Name : <s:property value="name"/><br>
Password : <s:property value="password"/>
</h2>
</body>
</html>

//***********************************************************************

loginFailure.jsp
//***********************************************************************

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login2.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>
<h1>登陆失败!</h1>
<h2>用户名或密码错误!</h2>
</body>
</html>

//***********************************************************************

6.注意别忘了导入STRUTS2的JAR包(用此5个即可)(放在WebRoot/WEB-INF/lib/)下,这是必须的.
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.2.jar
xwork-2.0.5.jar

7.如图1
同时也要就以上5个JAR包导入Referced Libraries里,这是在ECLIPSE 下编辑时用的.

8.此程序可以正常运行,当你配置好时在浏览器上写 http://localhost:8080/MyFirstStruts2/login.jsp 即可(默认是8080端口)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: