关注图老师设计创意栏目可以让大家能更好的了解电脑,知道有关于电脑的更多有趣教程,今天给大家分享开源技术——体验Struts教程,希望对大家能有一点小小的帮助。
【 tulaoshi.com - 编程语言 】
用户登陆的实现
看到题目,您一定觉得很土,Struts早已风靡,而关于Stuts的文章也早已遍地都是,假如你觉得土那你就别看了,我只是把我这段时间学到的一些比较肤浅知识在这里记录一下,假如您真在这些连载文章中获得了您想要的知识,那么我就会很欣慰了。import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class LoginForm extends ActionForm
{
private static final long serialVersionUID = 1L;
private String user = null;
private String password = null;
public String getPassword()
{
return password;
}
public void setPassword( String password )
{
this.password = password;
}
public String getUser()
{
return user;
}
public void setUser( String user )
{
this.user = user;
}
public void reset(ActionMapping mapping,HttpServletRequest request)
{
this.password = null;这里很重要,当用户输入有错时,需要返回登陆界面给用户,为了用户填写方便我们可以设置返回给用户的哪部分信息设置为空
}
}
我用来实现登陆的DispatchAction代码如下:
public ActionForward login( ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res ) throws Exception
{
Service service = getService();调用业务逻辑
LoginForm loginForm = (LoginForm) form;获取formbean
String user = loginForm.getUser();提取用户名
Person person = service.getUser( user );从业务逻辑中查找用户
ActionMessages messages = new ActionMessages();
ActionMessage am;
if ( person == null )假如用户不存在,我们就返回
{
am = new ActionMessage( "index.jsp.fail.user", user );参数的意义:第一个是主串,而后面的作为arg数组
messages.add( "user", am );把错误信息放到errors 属性为user那里去显示
saveErrors( req, messages );
form.reset( mapping, req );假如出现错误,调用formbean的重置功能
return mapping.findForward( ID.FAIL );
}
if ( !person.getPassword().equals( loginForm.getPassword() ) )假如密码不一致
{
am = new ActionMessage( "index.jsp.fail.password", user );
messages.add( "password", am );
saveErrors( req, messages );
form.reset( mapping, req );
return mapping.findForward( ID.FAIL );
}
setSessionObject( req, person.getType(), person );把用户放到session里
return new ActionForward( person.getType() + ".do", true );我在每个类型用户的类中加入了一个getType来在这里调用,之后动态的去对应的admin.do,student.do,teacher.do的主页面,并且这里实现的不是请求转发,而是请求从定向
}
整体结构
为了让大家更方便的了解我这个设计,我先把我的一些整体的规划都说出来吧,由于我是初学,难免会参照本书籍来看,我买的是那本孙某女的书《精通:*****》,看了看她前面的介绍,我一看了不得,能出书,写的还都不错,这女的可不得了,渐渐迷惑的地方非常多,比如例子里面注释都拽上了英语,搞不懂,而当我从网上下到电子盗版jakarta struts(我已安下栽说明要求的那样在24小时后删除了)这本书的时候我才恍然大悟,原来是抄袭啊?至于是谁抄的谁,口说无凭,不能乱诽谤,不过大家心里都该有杆称!
下面就是代码了:
package com.boya.subject.model;
public interface Person
{
public Long getId();
public void setId( Long id );
public String getName();
public void setName( String name );
public String getPassword();
public void setPassword( String password );
public String getTelphone();
public void setTelphone( String telphone );
public String getUser();
public void setUser( String user );
public String getType();
}
package com.boya.subject.model;
public abstract class User implements Person
{
private Long id;数据库id
private String user;用户名
private String password;密码
private String name;姓名
private String telphone;电话
public Long getId()
{
return id;
}
public void setId( Long id )
{
this.id = id;
}
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 getTelphone()
{
return telphone;
}
public void setTelphone( String telphone )
{
this.telphone = telphone;
}
public String getUser()
{
return user;
}
public void setUser( String user )
{
this.user = user;
}
}
package com.boya.subject.model;
public class Admin extends User
{
private String grade = null; 治理员权限
public String getGrade()
{
return grade;
}
public void setGrade( String grade )
{
this.grade = grade;
}
public String getType()
{
return "admin";
}
}
package com.boya.subject.model;
public class Teacher extends User
{
private String level; 教师职称
public String getLevel()
{
return level;
}
public void setLevel( String level )
{
this.level = level;
}
public String getType()
{
return "teacher";
}
}
package com.boya.subject.model;
public class Student extends User
{
private String sn;学生学号
private SchoolClass schoolClass; 班级
public SchoolClass getSchoolClass()
{
return schoolClass;
}
public void setSchoolClass( SchoolClass schoolClass )
{
this.schoolClass = schoolClass;
}
public String getSn()
{
return sn;
}
public void setSn( String sn )
{
this.sn = sn;
}
public String getType()
{
return "student";
}
}
而对于Action我分别做了一个抽象类,之后别的从这里继续
先是Action的
package com.boya.subject.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.boya.subject.frame.ID;
import com.boya.subject.frame.ServiceFactory;
import com.boya.subject.model.Person;
import com.boya.subject.service.Service;
import com.boya.subject.util.HtmlUtil;
public abstract class BaseAction extends Action
{
/**
* 由服务工厂方法创建服务
* @return 数据库操作的服务
* 2006-5-16 18:10:04
*/
public Service getService()
{
ServiceFactory factory = (ServiceFactory) getAppObject( ID.SF );
Service service = null;
try
{
service = factory.createService();
}
catch ( Exception e )
{
}
return service;
}
/**
* 判定用户是否合法登陆
* @param req
* @return 用户是否登陆
* 2006-5-16 18:11:26
*/
public boolean isLogin( HttpServletRequest req )
{
if ( getPerson( req ) != null ) return true;
else
return false;
}
/**
* 抽象方法,子类实现
* @param mapping
* @param form
* @param req
* @param res
* @return
* @throws Exception
* 2006-5-16 18:12:54
*/
protected abstract ActionForward executeAction( ActionMapping mapping,
ActionForm form, HttpServletRequest req, HttpServletResponse res )
throws Exception;
/**
* 获取session范围的用户
* @param req
* @return 当前用户
* 2006-5-16 18:13:35
*/
public abstract Person getPerson( HttpServletRequest req );
/**
* 父类的执行Action
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public ActionForward execute( ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res ) throws Exception
{
if ( !isLogin( req ) )
{
HtmlUtil.callParentGo( res.getWriter(), ID.M_UNLOGIN, ID.P_INDEX );
return null;
}
return executeAction( mapping, form, req, res );
}
/**
* 删除session中属性为attribute的对象
* @param req
* @param attribute 对象属性
* 2006-5-16 18:16:59
*/
public void removeSessionObject( HttpServletRequest req, String attribute )
{
HttpSession session = req.getSession();
session.removeAttribute( attribute );
}
/**
* 设置session中属性为attribute的对象
* @param req
* @param attribute 设置属性
* @param o 设置对象
* 2006-5-16 18:17:50
*/
public void setSessionObject( HttpServletRequest req, String attribute,
Object o )
{
req.getSession().setAttribute( attribute, o );
}
/**
* 设置application中属性为attribute的对象
* @param req
* @param attribute 设置属性
* @param o 设置对象
* 2006-5-16 18:17:50
*/
public void setAppObject( String attribute, Object o )
{
servlet.getServletContext().setAttribute( attribute, o );
}
public Object getSessionObject( HttpServletRequest req, String attribute )
{
Object obj = null;
HttpSession session = req.getSession( false );
if ( session != null ) obj = session.getAttribute( attribute );
return obj;
}
public Object getAppObject( String attribute )
{
return servlet.getServletContext().getAttribute( attribute );
}
public void callParentGo( HttpServletResponse res, String msg, String url )
throws IOException
{
HtmlUtil.callParentGo( res.getWriter(), msg, url );
}
public void callMeGo( HttpServletResponse res, String msg, String url )
throws IOException
{
HtmlUtil.callMeGo( res.getWriter(), msg, url );
}
public void callBack( HttpServletResponse res, String msg )
throws IOException
{
HtmlUtil.callBack( res.getWriter(), msg );
}
public void callMeConfirm( HttpServletResponse res, String msg, String ok,
String no ) throws IOException
{
HtmlUtil.callMeConfirm( res.getWriter(), msg, ok, no );
}
}
再是DispatchAction的
package com.boya.subject.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.boya.subject.frame.ID;
import com.boya.subject.frame.ServiceFactory;
import com.boya.subject.model.Person;
import com.boya.subject.service.Service;
import com.boya.subject.util.HtmlUtil;
public abstract class BaseDispatchAction extends DispatchAction
{
/**
* 由服务工厂方法创建服务
* @return 数据库操作的服务
* 2006-5-16 18:10:04
*/
public Service getService()
{
ServiceFactory factory = (ServiceFactory) getAppObject( ID.SF );
Service service = null;
try
{
service = factory.createService();
}
catch ( Exception e )
{
}
return service;
}
/**
* 判定用户是否合法登陆
* @param req
* @return 用户是否登陆
* 2006-5-16 18:11:26
*/
public boolean isLogin( HttpServletRequest req )
{
if ( getPerson( req ) != null ) return true;
else
return false;
}
/**
* 获取session范围的用户
* @param req
* @return 当前用户
* 2006-5-16 18:13:35
*/
public abstract Person getPerson( HttpServletRequest req );
/**
* 父类的执行DispatchAction
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public ActionForward execute( ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res ) throws Exception
{
try
{
if ( !isLogin( req ) )
{
callParentGo( res, ID.M_UNLOGIN, ID.P_INDEX );
return null;
}
return super.execute( mapping, form, req, res );
}
catch ( NoSUChMethodException e )
{
callBack( res, ID.M_NOMETHOD );
return null;
}
}
/**
* 删除session中属性为attribute的对象
* @param req
* @param attribute 对象属性
* 2006-5-16 18:16:59
*/
public void removeSessionObject( HttpServletRequest req, String attribute )
{
HttpSession session = req.getSession();
session.removeAttribute( attribute );
}
/**
* 设置session中属性为attribute的对象
* @param req
* @param attribute 设置属性
* @param o 设置对象
* 2006-5-16 18:17:50
*/
public void setSessionObject( HttpServletRequest req, String attribute,
Object o )
{
req.getSession().setAttribute( attribute, o );
}
/**
* 设置application中属性为attribute的对象
* @param req
* @param attribute 设置属性
* @param o 设置对象
* 2006-5-16 18:17:50
*/
public void setAppObject( String attribute, Object o )
{
servlet.getServletContext().setAttribute( attribute, o );
}
public Object getSessionObject( HttpServletRequest req, String attribute )
{
Object obj = null;
HttpSession session = req.getSession( false );
if ( session != null ) obj = session.getAttribute( attribute );
return obj;
}
public Object getAppObject( String attribute )
{
return servlet.getServletContext().getAttribute( attribute );
}
public void callParentGo( HttpServletResponse res, String msg, String url )
throws IOException
{
HtmlUtil.callParentGo( res.getWriter(), msg, url );
}
public void callMeGo( HttpServletResponse res, String msg, String url )
throws IOException
{
HtmlUtil.callMeGo( res.getWriter(), msg, url );
}
public void callBack( HttpServletResponse res, String msg )
throws IOException
{
HtmlUtil.callBack( res.getWriter(), msg );
}
public void callMeConfirm( HttpServletResponse res, String msg, String ok,
String no ) throws IOException
{
HtmlUtil.callMeConfirm( res.getWriter(), msg, ok, no );
}
}
对于程序中的一些提示信息,我比较喜欢用JS来写,所以我把这些都放到了一个类中
import java.io.IOException;
import java.io.Writer;
public class HtmlUtil
{
public static void callParentGo( Writer out, String msg, String url )
throws IOException
{
out.write( " " );
}
public static void callMeGo( Writer out, String msg, String url )
throws IOException
{
out.write( " " );
}
public static void callMeConfirm( Writer out, String msg ,String ok, String no )
throws IOException
{
out.write( " " );
}
public static void callBack( Writer out, String msg ) throws IOException
{
out.write( " " );
}
}
接着上次的话题,下面的就是学生注册时需要的学院,专业,班级,三层列表,
学院:
专业:
班级:
学院是上来就应该有的,我们把他放到了LabelValueBean里
public Vector
{
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
connection = getConnection();
pstmt = connection.prepareStatement( "select * from institute" );
rs = pstmt.executeQuery();
Vector
institutes.add( new LabelValueBean( "请选择所在学院", "" ) );
while ( rs.next() )
{
institutes.add( new LabelValueBean(
rs.getString( "institute" ), rs.getString( "id" ) ) );
}
return institutes;
}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
close( rs );
close( pstmt );
close( connection );
}
return null;
}
而当它选择了一个学院后,相应的getDepartments(this.value)的js脚本就该工作了,还是四步
var xmlHttp;
function createXMLHttpRequest()
{
if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
发出请求
function getDepartments(institute)
{
createXMLHttpRequest()
var url = "ajax.do?institute="+institute+"&method=getDepartments"
xmlHttp.open("GET",url, true)
xmlHttp.onreadystatechange = departments
xmlHttp.send(null)
}
处理响应
function departments()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
resText = xmlHttp.responseText
each = resText.split("")
buildSelect( each, document.getElementById("departmentId"), "请选择所在专业");
}
}
}
function buildSelect(str,sel,label)
{
sel.options.length=0;
sel.options[sel.options.length]=new Option(label,"")
for(var i=0;i
each=str[i].split(",")
sel.options[sel.options.length]=new Option(each[0],each[1])
}
}
我把从数据库中得到的各个专业进行了编码,之后再这里再回归回去,下面的是编码过程
public StringBuffer getDepartmentsByInstituteIdForAjax( Long instituteId )
{
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
connection = getConnection();
pstmt = connection
.prepareStatement( "select * from department where instituteID=?" );
pstmt.setLong( 1, instituteId );
rs = pstmt.executeQuery();
StringBuffer sb = new StringBuffer();
while ( rs.next() )
{
sb.append( rs.getString( "department" ) + ","
+ rs.getLong( "id" ) );
if ( !rs.isLast() ) sb.append( "" );
}
return sb;
}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
close( rs );
close( pstmt );
close( connection );
}
return null;
}
当然这些都是由
public ActionForward getDepartments( ActionMapping mapping,
ActionForm form, HttpServletRequest req, HttpServletResponse res )
throws Exception
{
Service service = getService();
res.getWriter().write(
service.getDepartmentsByInstituteIdForAjax(
Long.parseLong( req.getParameter( "institute" ) ) )
.toString() );
return null;
}
来控制
===========班级的再这里
public ActionForward getClasses( ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res ) throws Exception
{
Service service = getService();
res.getWriter().write(
service.getClassesByDepartmentIdForAjax(
Long.parseLong( req.getParameter( "department" ) ) )
.toString() );
return null;
}
public StringBuffer getClassesByDepartmentIdForAjax( Long departmentId )
{
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
connection = getConnection();
pstmt = connection
.prepareStatement( "select * from class where departmentID=?" );
pstmt.setLong( 1, departmentId );
rs = pstmt.executeQuery();
StringBuffer sb = new StringBuffer();
while ( rs.next() )
{
sb.append( rs.getString( "class" ) + "," + rs.getLong( "id" ) );
if ( !rs.isLast() ) sb.append( "" );
}
return sb;
}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
close( rs );
close( pstmt );
close( connection );
}
return null;
}
function getClasses(department)
{
createXMLHttpRequest()
var url = "ajax.do?department="+department+"&method=getClasses"
xmlHttp.open("GET",url, true)
xmlHttp.onreadystatechange = classes
xmlHttp.send(null)
}
function classes()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
resText = xmlHttp.responseText
each = resText.split("")
buildSelect( each, document.getElementById("classid"), "请选择所在班级");
}
}
}
大家都知道Struts是一种基于MVC的结构,而这个MVC又怎么样理解呢?书上阐述的一般都很具体,而我的理解很直白,我们可以把业务逻辑放到每个JSP页面中,当你访问一个JSP页面的时候,就可以看到业务逻辑得到的结果,而把这些业务逻辑与HTML代码夹杂到了一起,一定会造成一些不必要的麻烦,可以不可以不让我们的业务逻辑和那些HTML代码夹杂到一起呢?多少得搀杂一些,那干脆,尽量少的吧,于是我们可以尝试着把业务逻辑的运算过程放到一个Action里,我们访问这个Action,之后Action执行业务逻辑,最后把业务逻辑的结果放到request中,并将页面请求转发给一个用于显示结果的jsp页面,这样,这个页面就可以少去很多的业务逻辑,而只是单纯的去显示一些业务逻辑计算结果的页面而已。这时的Action称为控制器,JSP页可以叫做视图了,而控制器操作的业务对象,无非就应该叫模型了!
从上面的话,我们来分析一下当我们要做一个分页时所需要的部分,而在这之前,我们先看看他们的执行过程吧,首先我们第一次请求访问一个页面,它会把所有记录的前N条显示给我们,之后计算是否有下一页,等类似的信息,当我们点下一页的时候,就获取下一页的信息,我们还可以添加一个搜索,比如我们用于显示学生的,可以安学生姓名查找,学号查找,班级查找。而对于显示的对象,我们一般也都会封装为javabean,所以用于放置查询结果的容器是不定的,而这时,我们就需要用泛型来提升我们的代码效率!
首先我们写一个用于分页显示的javabean:
package com.boya.subject.model;
import java.util.Vector;
public class Page
{
private int current = 1; //当前页
private int total = 0; //总记录数
private int pages = 0; //总页数
private int each = 5; //每页显示
private int start = 0; //每页显示的开始记录数
private int end = 0; //每页显示的结束记录数
private boolean next = false; //是否有下一页
private boolean previous = false; //是否有上一页
private Vector
public Page( Vector
{
this.v = v;
each = per;
total = v.size(); //容器的大小就是总的记录数
if ( total % each == 0 )
pages = total / each; //计算总页数
else
pages = total / each + 1;
if ( current = pages )
{
next = false;
}
else
{
next = true;
}
if ( total each )
{
start = 0;
来源:http://www.tulaoshi.com/n/20160219/1622807.html
看过《开源技术——体验Struts》的人还看了以下文章 更多>>