今天把struts
这是我关于这个例子的流程或结构图:
struts-config.xml控制着整个流程的流转
AdminLoginForm负责数据的保存和传递
AdminLoginAction负责业务逻辑的处理
现在把测试的过程列出来:
1、程序运行环境:
Tomcat
Oracle
struts1.3.8
2、数据准备:
建立数据库:testperson,用于存储登陆用到的用户名、密码
- create table testperson(
- name char(20),
- password char(20)
- );
- insert into testperson values('admin','admin')
3、在Tomcat中新建一个虚拟目录:
在%TOMCAT_HOME%\conf\Catalina\localhost目录下新建一个文件:strutstest.xml
文件内容:
- <context path="/strutstest" docBase="d:/jsp/struts138" reloadable="true" crossContext="true" debug="0"></context>
这样就创建了一个虚拟目录
path为浏览器中的访问路径,应该与文件名保持一致
docBase为你的文件存放的位置
4、添加所需要的.jar包:
在d:/jsp/struts138目录下创建如下目录结构:
–WEB-INF
|
|—lib
|—classes
将下载到的struts中lib目录下的全部文件拷贝到此lib目录下
将下载到的struts中src\taglib\src\main\resources\META-INF\tld目录下的全部.tld文件拷贝到WEB-INF目录下
5、创建配置文件:
(1)web.xml
这是一个基本的配置,文件保存位置:WEB-INF\
关于每个配置节都起什么作用,网上可以找到详细说明,此处不再详述。
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!DOCTYPE web-app PUBLIC
- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <web-app>
- <display-name>Struts Blank Application</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>
- <load-on-startup>2</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <taglib>
- <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
- <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
- </taglib>
- </web-app>
(2)struts-config.xml
这是一个struts的配置,文件保存位置:WEB-INF\
关于每个配置节都起什么作用,网上可以找到详细说明,此处不再详述。
- <?xml version="1.0" encoding="UTF-8"?>
- <struts-config>
- <form-beans>
- <form-bean name="adminLoginForm" type="AdminLoginForm" />
- </form-beans>
- <global-forwards>
- <forward name="ToUserList" path="/userlist.jsp" />
- <forward name="ToErrorPage" path="/error.jsp" />
- </global-forwards>
- <action-mappings>
- <action path="/adminlogin" type="AdminLoginAction" name="adminLoginForm" scope="request" />
- </action-mappings>
- <message-resources parameter="ApplicationResources" />
- </struts-config>
6、创建jsp文件:
(1)adminlogin.jsp
登陆窗体,其中用到了tag标签,我们在web.xml中作了配置,同时在这里引用
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
同时文件中form的action指向我们在struts-config中作了配置
- <%@ page contentType="text/html;charset=GBK" language="java"%>
- <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
- <html>
- <head>
- <title>JSP for loginForm form</title>
- </head>
- <body>
- <html:form action="adminlogin.do" >
- <table>
- <tr>
- <td > 用户名 </td>
- <td > <html:text property="username" /> </td>
- </tr>
- <tr>
- <td > 密码</td>
- <td > <html:password property="password" /> </td>
- </tr>
- <tr>
- <td ><html:submit property="submit">登录</html:submit></td>
- <td ><html:reset> 重置</html:reset> </td>
- </tr>
- </table>
- </html:form>
- </body>
- </html>
(2)error.jsp
- <%@ page contentType="text/html;charset=GBK" language="java"%>
- <html>
- <head><title>登陆失败</title></head>
- <body>
- 登陆失败
- <html:errors />
- </body>
- </html>
(3)userlist.jsp
- <%@ page contentType="text/html;charset=GBK" language="java"%>
- <html>
- <head><title>登陆成功</title></head>
- <body>登陆成功</body>
- </html>
7、Java类
(1)AdminLoginForm.java
用于数据的保存和传递
- import org.apache.struts.action.ActionForm;
- public class AdminLoginForm extends ActionForm {
- /**
- *
- */
- private static final long serialVersionUID = 8189906596424257682L;
- private String username = null;
- private String password = null;
- public AdminLoginForm() {
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getUsername() {
- return username;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getPassword() {
- return password;
- }
- }
(2)AdminLoginAction.java
用于登陆验证
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.Statement;
- 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.ActionMessage;
- import org.apache.struts.action.ActionMessages;
- public final class AdminLoginAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception {
- //获取来自AdminLoginForm对象的数据。
- AdminLoginForm adminLoginForm = (AdminLoginForm) form;
- String username = adminLoginForm.getUsername();
- String password = adminLoginForm.getPassword();
- ActionMessages errors = new ActionMessages();
- String PageForward = "ToErrorPage";
- //只有用户名与密码相匹配,而且用户身份为管理员时才能进入系统
- //设置转发页面,struts-config. xml中<forward path="/"项目路径/userlist.jsp" />定义。
- Connection conn = DataBaseConn.getConn();
- // Create a Statement
- Statement stmt = conn.createStatement ();
- ResultSet rset = stmt.executeQuery ("select * from testperson where name='"+username+"' and password = '"+password+"'");
- if(rset.next()){
- PageForward = "ToUserList";
- } else {
- errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage( "error.admin.accessDeny")); //error.admin.accessDeny定义在ApplicationResource.properties
- if (!errors.isEmpty()){
- saveErrors(request, errors);
- }
- }
- conn.close();
- return mapping.findForward(PageForward);
- }
- }
(3)DataBaseConn.java
返回一个数据连接对象
- import java.sql.*;
- public class DataBaseConn
- {
- public static Connection getConn() throws SQLException{
- String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";//连接url
- String userName = "user";//用户名
- String password = "pass";//密码
- DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
- Connection conn = DriverManager.getConnection (url, userName, password);
- return conn;
- }
- }
(4)ApplicationResource.properties
属性文件,登陆失败时的错误记录信息
- error.admin.accessDeny=密码错误!
好了,这就是全部了!
初学,理解难免有偏差,欢迎大家就Struts方面的问题与我交流!
来源:https://www.cnblogs.com/buffer/archive/2009/11/29/1613242.html