OS:windows IDE:eclipse3.1/3.2 WEB SERVICE:tomcat5.5 JDK:1.5 Struts:1.2
1.open the eclipse and new a java project,named test1.
2.right click the new project(test1),and new a source folder named src,and then new the package in the src,named com.test.one.struts.action,and new a file named LoginAction.java within this package.
3.right click the test1,and new a folder named web,and create a new folder named WEB-INF within web folder,and new a lib folder within WEB-INF folder,and the path the project needed are finished.
4.copy the jar of the struts and servlet to the lib folder,and right click the project,and build path ->add Library->user library->new (named test_lib)->add jars,select the project's lib directory,and all the jar added to the project.
5.right click the WEB-INF,new two xml files,one named web.xml,the other named struts-config.xml,the content of web.xml is:
<?xml version="1.0" encoding="ISO-8859-1"?><web-app 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" version="2.4"> <display-name>Struts Tiles Documentation</display-name> <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>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
<!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
</web-app>The content of struts-config.xml is:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config> <form-beans> <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" size="15" type="java.lang.String" /> <form-property name="password" size="15" type="java.lang.String" /> </form-bean> </form-beans> <action-mappings> <action input="/login.jsp" name="loginForm" path="/loginAction" scope="request" type="com.test.one.struts.action.LoginAction"> <forward name="succ" path="/succ.jsp"/> <forward name="fail" path="/login.jsp"/> </action> </action-mappings> <!--message-resources parameter="ApplicationResources" /--> </struts-config>6.new a jsp file named login.jsp in the web folder,and the content:
<%@ page contentType="text/html; charset=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""http://www.w3.org/TR/html4/loose.dtd"><html:html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>管理登录</title><link href="css/admin.css" rel="stylesheet" type="text/css"></head>
<body><p> </p><table width="400" border="0" align="center" cellpadding="15" cellspacing="0" class="table1"> <html:form action="/loginAction" target="_top" method="post"> <tr> <td> <table width="70%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td colspan="2"><strong>管理登录:请输入您的帐号和密码</strong></td> </tr> <tr> <td colspan="2"> <div class="errormsg" id="errors"> <html:errors/> </div> </td> </tr> <tr> <td width="35%"><div align="right">用户名:</div></td> <td width="65%"><html:text property="username" styleClass="input2" size="15"></html:text></td> </tr> <tr> <td><div align="right">密码:</div></td> <td><html:password property="password" styleClass="input2" size="15"></html:password></td> </tr> <tr> <td colspan="2"><div align="center"> <html:submit styleClass="button1">登录</html:submit> <html:reset styleClass="button1">重置</html:reset> </div></td> </tr> </table> </td> </tr> </html:form></table>
</body></html:html>and then new a succ.jsp in the same folder :
<%@ page contentType="text/html; charset=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""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>管理登录</title><link href="css/admin.css" rel="stylesheet" type="text/css"></head>
<body>success</body></html>
7.the LoginAction.java in the package com.test.one.struts.action is:
package com.test.one.struts.action;
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 org.apache.struts.action.DynaActionForm;
public class LoginAction extends Action{ public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){ String forward="fail"; DynaActionForm uf = (DynaActionForm ) actionForm; String name=(String)uf.getString("username"); String pwd=(String)uf.getString("password"); if(name.equals("test") && pwd.equals("test")){ forward="succ"; System.out.println("@@@@@@@@@@@@@@@error"); } System.out.println("@@@@@@@@@@@@@@@success"); return actionMapping.findForward(forward); } }
8,right click the test1->properties->Java Build Path->default output folder,select the WEB-INF directory,and new a folder named classes,and OK,config the tomcat ,restart ,input the website:http://localhost:8080/test1/login.jsp,and you can try it
OS:windows IDE:eclipse3.1/3.2 WEB SERVICE:tomcat5.5 JDK:1.5 Struts:1.2
1.open the eclipse and new a java project,named test1.
2.right click the new project(test1),and new a source folder named src,and then new the package in the src,named com.test.one.struts.action,and new a file named LoginAction.java within this package.
3.right click the test1,and new a folder named web,and create a new folder named WEB-INF within web folder,and new a lib folder within WEB-INF folder,and the path the project needed are finished.
4.copy the jar of the struts and servlet to the lib folder,and right click the project,and build path ->add Library->user library->new (named test_lib)->add jars,select the project's lib directory,and all the jar added to the project.
5.right click the WEB-INF,new two xml files,one named web.xml,the other named struts-config.xml,the content of web.xml is:
<?xml version="1.0" encoding="ISO-8859-1"?><web-app 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" version="2.4"> <display-name>Struts Tiles Documentation</display-name> <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>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
<!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
</web-app>The content of struts-config.xml is:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config> <form-beans> <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" size="15" type="java.lang.String" /> <form-property name="password" size="15" type="java.lang.String" /> </form-bean> </form-beans> <action-mappings> <action input="/login.jsp" name="loginForm" path="/loginAction" scope="request" type="com.test.one.struts.action.LoginAction"> <forward name="succ" path="/succ.jsp"/> <forward name="fail" path="/login.jsp"/> </action> </action-mappings> <!--message-resources parameter="ApplicationResources" /--> </struts-config>6.new a jsp file named login.jsp in the web folder,and the content:
<%@ page contentType="text/html; charset=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""http://www.w3.org/TR/html4/loose.dtd"><html:html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>管理登录</title><link href="css/admin.css" rel="stylesheet" type="text/css"></head>
<body><p> </p><table width="400" border="0" align="center" cellpadding="15" cellspacing="0" class="table1"> <html:form action="/loginAction" target="_top" method="post"> <tr> <td> <table width="70%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td colspan="2"><strong>管理登录:请输入您的帐号和密码</strong></td> </tr> <tr> <td colspan="2"> <div class="errormsg" id="errors"> <html:errors/> </div> </td> </tr> <tr> <td width="35%"><div align="right">用户名:</div></td> <td width="65%"><html:text property="username" styleClass="input2" size="15"></html:text></td> </tr> <tr> <td><div align="right">密码:</div></td> <td><html:password property="password" styleClass="input2" size="15"></html:password></td> </tr> <tr> <td colspan="2"><div align="center"> <html:submit styleClass="button1">登录</html:submit> <html:reset styleClass="button1">重置</html:reset> </div></td> </tr> </table> </td> </tr> </html:form></table>
</body></html:html>and then new a succ.jsp in the same folder :
<%@ page contentType="text/html; charset=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""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>管理登录</title><link href="css/admin.css" rel="stylesheet" type="text/css"></head>
<body>success</body></html>
7.the LoginAction.java in the package com.test.one.struts.action is:
package com.test.one.struts.action;
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 org.apache.struts.action.DynaActionForm;
public class LoginAction extends Action{ public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){ String forward="fail"; DynaActionForm uf = (DynaActionForm ) actionForm; String name=(String)uf.getString("username"); String pwd=(String)uf.getString("password"); if(name.equals("test") && pwd.equals("test")){ forward="succ"; System.out.println("@@@@@@@@@@@@@@@error"); } System.out.println("@@@@@@@@@@@@@@@success"); return actionMapping.findForward(forward); } }
8,right click the test1->properties->Java Build Path->default output folder,select the WEB-INF directory,and new a folder named classes,and OK,config the tomcat ,restart ,input the website:http://localhost:8080/test1/login.jsp,and you can try it