5、创建form数据对象
打开file->new->package对话框,name中输入com.is.form,点击finish按钮。在右边的package explorer树中找到刚才创建的包,右键点击com.is.form包,菜单中的new->others,找到amateras->struts->struts action form,点击next,在对话框中name栏输入loginform,点击finish按钮。
编辑loginform类的内容为:
package com.is.form;
import org.apache.struts.action.actionform;
public class loginform extends actionform
{
private static final long
serialversionuid = 1l;
private string username = "";
private string password = "";
/**
* @return returns the password.
*/
public string getpassword()
{
return password;
}
/**
* @param password the password to set.
*/
public void setpassword(string password)
{
this.password = password;
}
/**
* @return returns the username.
*/
public string getusername()
{
return username;
}
/**
* @param username the username to set.
*/
public void setusername(string username)
{
this.username = username;
}
}
注意,这里的两个属性分别对应我们jsp中form中的两个输入控件的名称,为什么这样做,可以去看struts的帮助文档了,我就不详细说了,还有form类再写完属性后,get和set方法可以通过eclipse的source中的命令来自动生成,在右键菜单中,也不详细说了,去网上查资料吧,关于eclipse的使用有很多的文档。
七、安装eclipse html editor插件
解压缩tk.eclipse.plugin.htmleditor_1.6.7.zip包,然后将plugins目录拷贝至d:/eclipse目录下覆盖原文件夹即可。到此eclipse html editor插件安装完成。
八、安装strutside插件
解压缩tk.eclipse.plugin.struts_1.1.7.zip包,然后将plugins目录拷贝至d:/eclipse目录下覆盖原文件夹即可。
好了,到此strutside插件安装完成。
6、创建action对象
同创建form的过程相同,我们只是新建一个com.is.action包,同样的过程,打开新建向导,只是选择struts action,创建loginaction.java类,均选默认值。我们编辑loginaction为如下内容:
package com.is.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.is.form.loginform;
public class loginaction extends action
{
private static final long serialversionuid = 1l;
public actionforward execute
(actionmapping mapping,
actionform form,
httpservletrequest request,
httpservletresponse response)
throws exception {
// this line is here for when the
input page is upload-utf8.jsp,
// it sets the correct character
encoding for the response
string encoding = request.getcharacterencoding();
if ((encoding != null) &&
(encoding.equalsignorecase("gb2312")))
{
response.setcontenttype
("text/html; charset=gb2312");
} else {
response.setcontenttype
("text/html; charset=gbk");
}
try {
if (form instanceof loginform)
{
loginform theform = (loginform) form;
if(theform.getusername().equals("user") &&
theform.getpassword().equals("123456"))
{
return new actionforward("/welcome.do?type=true");
}
else {
return new actionforward("/welcome.do?type=false");
}
}
} catch (exception e)
{
}
// this shouldn't happen in this example
return null;
}
}
注意这里是直接用actionforward转向的,你也可以按照struts中提供的空白例程struts-blank.war中的做法进行转向,可以比较一下会有收获的。
7、创建登录成功页面
同创建index.jsp页面相同,我们创建welcome.jsp页面,均使用默认设置。并编辑其内容如下:
<%@page pageencoding="gbk"
contenttype="text/html;
charset=gbk" %>
<html>
<head>
<meta http-equiv="content-type"
content="text/html;
charset=gbk"/>
<title></title>
</head>
<body>
<%
string type = request.getparameter("type");
if(type!=null&&type.equals("true")){
out.print("欢迎您的光临!");
}
else{
out.print("对不起,你输入的用户名或者密码错误!");
}
%>
</body>
</html>
8、增加struts-config.xml中的配置
添加formbean的配置,在和标签之间加入:
<form-bean
name="loginform"
type="com.is.form.loginform"/>
添加jsp文件的映射,在和标签之间加入:
<action
path="/index"
forward="/index.jsp"/>
<action
path="/welcome"
forward="/welcome.jsp"/>
添加action文件的映射,在和标签之间加入:
path="/logincheck"
type="com.is.action.loginaction"
name="loginform"
scope="request"
validate="true"/>
修改后的struts-config.xml大致如下形式:
<?xml version="1.0"?>
<!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>
<data-sources>
</data-sources>
<form-beans>
<form-bean
name="loginform"
type="com.is.form.loginform"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action
path="/index"
forward="/index.jsp"/>
<action
path="/welcome"
forward="/welcome.jsp"/>
<action
path="/logincheck"
type="com.is.action.loginaction"
name="loginform"
scope="request"
validate="true"/>
</action-mappings>
<controller processorclass=
"org.apache.struts.tiles.tilesrequestprocessor"/>
<message-resources parameter="messageresources"/>
<plug-in classname=
"org.apache.struts.tiles.tilesplugin">
<set-property property="definitions-config"
value="/web-inf/tiles-defs.xml"/>
<set-property property="moduleaware" value="true"/>
</plug-in>
<plug-in classname=
"org.apache.struts.validator.validatorplugin">
<set-property property="pathnames"
value="/web-inf/validator-rules.xml,
/web-inf/validation.xml"/>
</plug-in>
</struts-config>
到此我们可以运行测试程序了。
9、运行测试程序
右键点击testweb工程根目录,点击菜单中的tomcate project->update context definition,将工程部署进tomcat,成功后会提示操作成功。
点击菜单栏中的雄猫图标启动tomcat,然后在ie地址栏中输入http://localhost:8080/testweb/index.do,我们会看到index.jsp的页面内容。
闽公网安备 35060202000074号