第一個Struts2的實例:
第一個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端口)
本實例的開發工具及語言: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端口)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.