1,新建一个web项目,起名随便,例如(TestToken)
2.添加Struts支持
点击此项目(TestToken).右击到MyEclipse→Add Struts Capabilties...
点击Add Struts Capabilties...之后,在WEB-INF文件夹下会自动生成很多文件,其中包括struts-config.xml和web.xml
3.建设index.jsp页面
在页面引入stuts-html.tld标签文件,前缀为html
代码为:<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
页面编码用struts,<html:html></html:html>
写一个超链接:<html:link href="login.do?operator=doLink">login.jsp</html:link>
特别注意要有一个作为参数传递,为项目用的是operator,至于后面的doLink,以及login.do在后面会有详细说明。
整个页面下来的代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<head>
<title>index.jsp</title>
</head>
<body>
<html:link href="login.do?operator=doLink">login.jsp</html:link>
</body>
</html:html>
4.新建一个登录页面,login.jsp
同样需要引入:<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
在它的form表单提交的时候,它的提交代码为:<html:form action="login.do?operator=doLogin" method="post">
代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html>
<head>
<title>login.jsp</title>
</head>
<body>
<html:form action="login.do?operator=doLogin" method="post">
用户名:<html:text property="uname"/><br>
密码:<html:password property="upass"/><br>
<html:submit value="确认"/><br>
</html:form>
</body>
</html:html>
5.UserForm
我们新建一个form,用来做login.jsp登录的数据收集和其他处理
点击此项目(TestToken).右击到New→Other...→找到Struts1.2 Form,点击Next按钮
在弹出的Struts 1.2 Form Declaration中编写信息
Config/Module:指向的是我们这个项目的struts-config.xml的位置
Name:指向struts-config.xml中<form-beans>→<form-bean>中的name属性(如:userForm)
superclass:org.apache.struts.action.ActionForm
Form type:写明的是包名,与类名(如:com.Lure.web.form.UserForm),最后一个为Form类的类名,前面的都为包
点击Finish按钮
在src文件夹下自动生成了com.Lure.web.form包,以及 UserForm类
给UserForm类增加“用户名”和“密码”两个字段,以及他们的get和set方法
整个UserForm的代码如下:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.Lure.web.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 10-18-2009
*
* XDoclet definition:
* @struts.form name="userForm"
*/
public class UserForm extends ActionForm {
private String uname;
private String upass;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
}
validate和reset方法是自动生成的,是可选的。
在struts-config.xml中也会自动生成如下代码:
<form-beans >
<form-bean name="userForm" type="com.Lure.web.form.UserForm" />
</form-beans>
其中userForm就是Struts 1.2 Form Declaration的"Name:"
6.UserAction
我们新建一个action,用来做login.jsp登录的提交处理
点击此项目(TestToken).右击到New→Other...→找到Struts1.2 Action,点击Next按钮
在弹出的Struts 1.2 Action Declaration中编写信息
Config/Module:指向的是我们这个项目的struts-config.xml的位置
Path:指向login.jsp中<html:form中的action属性(如:/login.do)
superclass:org.apache.struts.actions.DispatchAction
Form type:写明的是包名,与类名(如:com.Lure.web.action.UserAction)
在Forwards可选项中点击Add按钮,增加页面转向信息
例如:一个Name为success,Path为welcome.jsp
一个Name为login,Path为login.jsp
再加一个Name为error,Path为error.jsp
既是我们要他在验证成功之后就跳转到welcome.jsp页面,失败之后就跳转到error.jsp页面
点击Finish按钮
在src文件夹下自动生成了com.Lure.web.action包,以及 UserAction类
在struts-config.xml文件中,也生成了如下代码:
<action-mappings >
<action path="/login" name="userForm" type="com.Lure.web.action.UserAction" parameter="operator" >
<forward name="error" path="/error.jsp" />
<forward name="success" path="/welcome.jsp" />
<forward name="login" path="/login.jsp" />
</action>
</action-mappings>
注意parameter属性,就是我们在每个页面的提交代码中所带的参数,在这里要给他配上,但为什么要这么配呢?
来看看UserAction的代码:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.Lure.web.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
/**
* MyEclipse Struts
* Creation date: 10-18-2009
*
* XDoclet definition:
* @struts.action validate="true"
* @struts.action-forward name="error" path="error.jsp"
* @struts.action-forward name="success" path="welcome"
*/
public class UserAction extends DispatchAction {
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward doLink(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
this.saveToken(request);
return mapping.findForward("login");
}
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward doLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String ret="error";
if(this.isTokenValid(request,false)){
if(uname.equals("Lure")&& upass.equals("Lure")){
ret="success";
}
}
this.saveToken(request);//必须重新做令牌
return mapping.findForward(ret);
}
}
我们在index.jsp超链接是,operator为doLink,页面超链接一点击,一提交就来到UseAction的doLink方法,
方法中,this.saveToken(request)是关键代码,它主要是把请求request保存到令牌中。方法执行完,我们跳
转到login.jsp页面。此时我们在网页右击→查看源文件,看到的代码是:
<html>
<head>
<title>login.jsp</title>
</head>
<body>
<form name="userForm" method="post" action="/TestToken/login.do?operator=doLogin"><div>
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="02cfae5da6cab067a577c6838e8feb6f"></div>
用户名:<input type="text" name="uname" value=""><br>
密码:<input type="password" name="upass" value=""><br>
<input type="submit" value="确认"><br>
</form>
</body>
</html>
源代码多出了<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="02cfae5da6cab067a577c6838e8feb6f">这行代码
其中value是我们每次提交的一个ID串号,串号是随机生成的
当我们登录名与密码输入为Lure,提交就到了UserAction的doLogin方法,如果(this.isTokenValid(request,false))==true,
以及用户名,密码验证成功,就到了welcome.jsp页面,失败就到了error.jsp页面,最后记得让页面的sessionID串号再刷一遍,
代码为 this.saveToken(request).(welcome.jsp和error.jsp可事先做好。)
登录成功,到了welcome.jsp页面。我们后退回来,再看登录界面的源文件,value依然是"02cfae5da6cab067a577c6838e8feb6f,
但是当我们在输入用户名,密码再提交时已经不能提交了,其实value不变,是因为我们是后退来查看,而实际上,我们已经把value改变了,代码就是在doLogin方法中的this.saveToken(request);//必须重新做令牌
令牌,防止不断刷新页面,反复提交就个项目就到此小结。