开始学struts框架了,有了前面的基础,学这个不是太难
【用jsp+javabean+servlet写过MVC的应该很快能掌握】
今天用最最基本的登陆流程解读下struts的基本框架
开发环境:myeclipse7.0+tomcat6.0+mysql5.0
1.新建工程及基本配置
【新建】-【project】-【web project】 取名bbs
【点右边的工程】 -【右键】-【MyEclipse】-【Add struts Capablilities】
自动生成web.xml配置
加条代码,将接入网页改为login.jsp
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
就不用web.xml文件了
2.新建以下包
说明:1.【actionForm】中的文件相当于bean
2.【action】存储action用于转发,类似于servlet
3.【dao】中存储的文件用于调用数据库类与数据库交互,返回结果给action用于跳转判断
4.【database】数据库连接类,专职与数据库操作
3.登陆的需要的三个类,User.java UserAction.java UserDAO.java
package actionForm;
import org.apache.struts.action.ActionForm;
import java.util.Vector;
public class User extends ActionForm {
private String userName="";//用户名
private String password="";//密码
public String getuserName() {
return userName;
}
public void setuserName(String name) {
userName = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.action;
import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.actionForm.ManagerForm;
import com.dao.ManagerDAO; //对应的manager数据库操作类
import javax.servlet.http.HttpSession;
public class UserAction extends Action {
private UserDAO userDAO = null;
public UserAction() {
this.userDAO = new UserDAO(dataSource) ;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String action = request.getParameter("action");
System.out.println("获取的查询字符串:" + action);
if (action == null || "".equals(action)) {
return mapping.findForward("error");
} else if ("login".equals(action)) {
return userLogin(mapping, form, request, response);
}
request.setAttribute("error", "操作失败!");
return mapping.findForward("error");
}
public ActionForward userLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
this.userDAO = new UserDAO() ;
User user= (User) form;
user.setuserName(user.getuserName());
user.setPassword(user.getPassword());
int flag = userDAO.verifyUser(user);//调用交互类,获取数据库返回值
if (flag == 1) {
HttpSession session = request.getSession();
session.setAttribute("user", user.getuserName());
return mapping.findForward("userloginok");
} else {
ActionMessages ams = new ActionMessages(); //消息传递机制,具体可以去查下,过几天再发
ams.add("error",new ActionMessage("登录失败!!!!!",false));
this.addMessages(request,ams); //区别
return mapping.findForward("error");
}
}
}
package dao;
import java.sql.ResultSet;
import database.DBConnect;
import actionForm.User;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDAO {
public UserDAO()
{
}
//判断用户名密码是否符合~~~~~~用于登陆判断,符合,返回1,否则返回0
public int verifyUser(User user){
int flag=0;
String result="";
String sql = "SELECT pwd FROM user where name='" +user.getuserName()+"'";
Connection connect = null;
String strSql;
ResultSet rs;
try{
connect = dataSource.getConnection();
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
result=rs.getString(1);
}
if(result.equals(user.getPassword()))
{
flag=1;
}
rs.close();
stmt.close();
connect.close();
//dbc.close();
}catch(Exception e)
{
flag=0;
e.printStackTrace();
}
return flag;
}
}
4.在mysql中建立数据库bbs,并建表user 内容user,pwd 输入个账号密码
在工程中build path中引入JDBC连接的jar文件
写个类测试一下是否连接成功
5.数据库交互类!!!
6.struts-config配置文件 注意各个命名,仔细分析下转发路径,整体流程配置就在这个文件中了
【type为类所在路径 如User.java在actionForm的User中】
【path为类转发路径】
<?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="user" type="actionForm.User" />
</form-beans>
<action-mappings type="org.apache.struts.action.ActionMapping">
<action name="user" path="/user" scope="request" type="action.UserAction" validate="true" input="/messagepage.jsp">
<forward name="userloginok" path="/index.jsp?para=1" />
<forward name="registerok" path="/index.jsp?para=2" />
</action>
</action-mappings>
<global-forwards>
<forward name="error" path="/message.jsp" />
</global-forwards>
</struts-config>
7.几个jsp文件
【login.jsp】
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 'login.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>
<form name="login" method="post" action="user.do?action=login">
<center>
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td>
<input type="submit" value="登陆"/>
<input type="reset" value="重置"/>
</td>
<td> <input type="button" value="退出" οnclick="javascript:window.close()"/>
<input type="button" value="注册" οnclick="javascript:window.location='register.jsp'"/>
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
【message.jsp】
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ 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"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
<head>
<title>
消息页面
</title>
</head>
<link href="images/myStyleClass.css" rel="stylesheet" type="text/css">
<body>
<center>
错误消息:
<html:messages id="message" message="true" property="error">
<bean:write name="message"/>
<br />
</html:messages>
<!-- 这段代码的输出结果为:
addMessages : info message
-->
<h2>
<a href="javascript:history.go(-1)">
返回上一页
</a>
</h2>
</center>
<hr>
</body>
</html:html>
【index.jsp】
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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 'index.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>
操作状态:${param.para }
<c:if test="${param.para==1}">
登陆成功!!!!!!!!
</c:if>
<c:if test="${param.para==2}">
注册成功!!!!!!!!
</c:if>
</body>
</html>
总结:以上只是通过一个最简单的登陆流程大概介绍了struts的基本结构
并不是很难,以struts-config.xml为中心,理解各个类的作用及如何配置
在模仿下各个类的处理方法
当然最基本的掌握了可以去看看一些更有用的东西,例如验证,消息传递,数据源等等
转载请注明:稻草誓言http://blog.sian.com.cn/programbus