Struts 1.3.8 学习笔记(一)
最近公司内部要搞一个SSH培训,而且Struts选的是1,而不是Struts2。由于版权的原因,不能使用收费的MyEclipse,而要使用开源免费的eclipse,网上现在基本都是Struts2的文章,所以把最近学习的过程记录一下,以备以后查阅!
准备环境
JDK:1.6+(其实1.7也没有问题)
Tomcat: apache-tomcat-6.0.33 (其实7.x也没有问题)
eclipse: eclipse-jee-juno-SR2-win32-x86_64.zip
Step1:
安装JDK;
Step2:
解压eclipse;如果JDK是绿色版的,在eclipse需要配置JRE(eclipse->Window->Preference->Java->Installed JREs,点击右边的【Add】,然后指定绿色版JDK的路径)
Step3:
解压Tomcat,在eclipse中配置此Server;
(eclipse->Window->Preference->Server->Runtimes Environment,点击右边的【Add】,然后指定Tomcat根目录的路径)
创建新工程
安装下面的步骤创建新工程:
File -> New -> Dynamic Web Project:
Project name: MyStruts01
Target runtime: Apache Tomcat v6.0(默认)
Dynamic web module version: 选 2.5 (这里我们不选默认的3.0)
拷贝jar档到工程的lib文件下(/MyStruts01/WebContent/WEB-INF/lib/)
第一部分(Struts相关):
antlr-2.7.2.jar
commons-beanutils-1.7.0.jar
commons-chain-1.1.jar
commons-digester-1.8.jar
commons-fileupload-1.1.1.jar
commons-io-1.1.jar
commons-logging-1.0.4.jar
commons-validator-1.3.1.jar
struts-core-1.3.8.jar
struts-el-1.3.8.jar
struts-extras-1.3.8.jar
struts-faces-1.3.8.jar
struts-mailreader-dao-1.3.8.jar
struts-scripting-1.3.8.jar
struts-taglib-1.3.8.jar
struts-tiles-1.3.8.jar
第二部分(数据库相关):
mysql-connector-java-5.1.17-bin.jar
创建相关文件
Action文件:
LoginAction.java
Form文件:
LoginForm.java
jsp文件:
login.jsp
success.jsp
failing.jsp
配置文件:
web.xml
struts-config.xml
工程文件结构
代码详细内容如下:
LoginAction.java
package com.coderdream.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 com.coderdream.form.LoginForm; public class LoginAction extends Action { /** * 处理客户端请求 */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 得到客户的的提交数据 LoginForm lf = (LoginForm) form; // 业务调用 if (lf.getUserName().equals("admin") && lf.getPassword().equals("admin")) { // 用户名密码验证成功,跳转到成功页面 return mapping.findForward("success"); } else { // 用户名密码错误,跳转到失败页面 return mapping.findForward("failing"); } } }
LoginForm.java
package com.coderdream.form; import org.apache.struts.action.ActionForm; public class LoginForm extends ActionForm { private String userName;// 用户名 private String password;// 密码 public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
jsp文件:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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"> <title>Insert title here</title> </head> <body> <form action="login.do"> <table align="center" border="1"> <tr bgcolor="#CCCCCC"> <th colspan="2">用户登录</th> </tr> <tr> <td>用户名:</td> <td><input type="text" name="userName" /></td> </tr> <tr> <td>密 码:</td> <td><input type="text" name="password" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="登录" /> </td> </tr> </table> </form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> </head> <body> 用户登录成功! </body> </html>
failing.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> </head> <body> 用户登录失败! </body> </html>
配置文件:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>MyStruts01</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="com.coderdream.form.LoginForm" /> </form-beans> <action-mappings> <action path="/login" type="com.coderdream.action.LoginAction" name="loginForm" input="login.jsp" scope="request"> <forward name="success" path="/success.jsp" /> <forward name="failing" path="/failing.jsp" /> </action> </action-mappings> </struts-config>
代码说明:
1、Action中通过mapping.findForward("success");跳转,这里的success字符串和struts-config.xml里配置相对应;
2、struts-config.xml的配置都是标准配置,可以参考官方的例子(\struts-1.3.8\apps\struts-cookbook-1.3.8\WEB-INF\struts-config.xml),包括action和forward的配置
运行结果:
输入正确的用户名密码:
登录成功
如果输入错误的用户名密码,则登录失败
源代码和库文件(由于后面的笔记使用相同的jar,所以只在这里上传一次):