创建form数据对象
打开File>new>package对话框name中输入comisform点击Finish按钮在右边的PackageExplorer树中找到刚才创建的包右键点击comisform包菜单中的new>others找到Amateras>struts>Struts Action Form点击next在对话框中name栏输入LoginForm点击Finish按钮
编辑LoginForm类的内容为
package comisform;
import orgapachestrutsactionActionForm;
public class LoginForm extends ActionForm
{
private static final long
serialVersionUID = L;
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)
{
thispassword = password;
}
/**
* @return Returns the username
*/
public String getUsername()
{
return username;
}
/**
* @param username The username to set
*/
public void setUsername(String username)
{
thisusername = username;
}
}
注意这里的两个属性分别对应我们jsp中form中的两个输入控件的名称为什么这样做可以去看struts的帮助文档了我就不详细说了还有form类再写完属性后get和set方法可以通过eclipse的source中的命令来自动生成在右键菜单中也不详细说了去网上查资料吧关于eclipse的使用有很多的文档
七安装Eclipse HTML Editor插件
解压缩tleditor_zip包然后将plugins目录拷贝至D:\eclipse目录下覆盖原文件夹即可到此Eclipse HTML Editor插件安装完成
八安装StrutsIDE插件
解压缩tkeclipsepluginstruts_zip包然后将plugins目录拷贝至D:\eclipse目录下覆盖原文件夹即可
好了到此StrutsIDE插件安装完成
创建action对象
同创建form的过程相同我们只是新建一个comisaction包同样的过程打开新建向导只是选择Struts Action创建LoginActionjava类均选默认值我们编辑LoginAction为如下内容
package comisaction;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgapachestrutsactionAction;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionForward;
import orgapachestrutsactionActionMapping;
import comisformLoginForm;
public class LoginAction extends Action
{
private static final long serialVersionUID = L;
public ActionForward execute
(ActionMapping mapping
ActionForm form
HttpServletRequest request
HttpServletResponse response)
throws Exception {
// this line is here for when the
input page is uploadutfjsp
// it sets the correct character
encoding for the response
String encoding = requestgetCharacterEncoding();
if ((encoding != null) &&
(encodingequalsIgnoreCase(GB)))
{
responsesetContentType
(text/html; charset=GB);
} else {
responsesetContentType
(text/html; charset=GBK);
}
try {
if (form instanceof LoginForm)
{
LoginForm theForm = (LoginForm) form;
if(theFormgetUsername()equals(user) &&
theFormgetPassword()equals())
{
return new ActionForward(/welcomedo?type=true);
}
else {
return new ActionForward(/welcomedo?type=false);
}
}
} catch (Exception e)
{
}
// this shouldnt happen in this example
return null;
}
}
注意这里是直接用ActionForward转向的你也可以按照struts中提供的空白例程strutsblankwar中的做法进行转向可以比较一下会有收获的
创建登录成功页面
同创建indexjsp页面相同我们创建welcomejsp页面均使用默认设置并编辑其内容如下
<%@page pageEncoding=GBK
contentType=text/html;
charset=GBK %>
<html>
<head>
<meta httpequiv=ContentType
content=text/html;
charset=GBK/>
<title></title>
</head>
<body>
<%
String type = requestgetParameter(type);
if(type!=null&&typeequals(true)){
outprint(欢迎您的光临!);
}
else{
outprint(对不起你输入的用户名或者密码错误!);
}
%>
</body>
</html>
增加Strutsconfigxml中的配置
添加formbean的配置在和标签之间加入
<formbean
name=loginForm
type=comisformLoginForm/>
添加jsp文件的映射在和标签之间加入
<action
path=/index
forward=/indexjsp/>
<action
path=/welcome
forward=/welcomejsp/>
添加action文件的映射在和标签之间加入
path=/logincheck
type=comisactionLoginAction
name=loginForm
scope=request
validate=true/>
修改后的strutsconfigxml大致如下形式
<?xml version=?>
<!DOCTYPE strutsconfig PUBLIC
//Apache Software Foundation
//DTD Struts Configuration //EN
/strutsconfig__dtd>
<strutsconfig>
<datasources>
</datasources>
<formbeans>
<formbean
name=loginForm
type=comisformLoginForm/>
</formbeans>
<globalexceptions>
</globalexceptions>
<globalforwards>
</globalforwards>
<actionmappings>
<action
path=/index
forward=/indexjsp/>
<action
path=/welcome
forward=/welcomejsp/>
<action
path=/logincheck
type=comisactionLoginAction
name=loginForm
scope=request
validate=true/>
</actionmappings>
<controller processorClass=
orgapachestrutstilesTilesRequestProcessor/>
<messageresources parameter=MessageResources/>
<plugin className=
orgapachestrutstilesTilesPlugin>
<setproperty property=definitionsconfig
value=/WEBINF/tilesdefsxml/>
<setproperty property=moduleAware value=true/>
</plugin>
<plugin className=
orgapachestrutsvalidatorValidatorPlugIn>
<setproperty property=pathnames
value=/WEBINF/validatorrulesxml
/WEBINF/validationxml/>
</plugin>
</strutsconfig>
到此我们可以运行测试程序了
运行测试程序
右键点击testweb工程根目录点击菜单中的Tomcate project>update context definition将工程部署进tomcat成功后会提示操作成功
点击菜单栏中的雄猫图标启动tomcat然后在IE地址栏中输入我们会看到indexjsp的页面内容