Eclipse开发struts完全指南(2)

2008-02-23 07:54:43来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

<%@page pageEncoding="GBK" contentType="text/html; charset=gb2312" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> <title></title> </head> <body> <form name="form1" method="post" action="/testweb/logincheck.do"> <table width="300" border="0" cellspacing="0" cellpadding="0"> <tr align="center"> <td colspan="2">用户登录信息</td> </tr> <tr> <td>用户名</td> <td> <input name="username" type="text" id="username" size="12"> user </td> </tr> <tr> <td>用户密码</td> <td> <input name="password" type="text" id="password" size="12"> 123456 </td> </tr> <tr align="center"> <td colspan="2"><input type="submit" name="Submit" value="提交"></td> </tr> </table> </form> </body> </html>

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; 

			  

			 } 

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:jsp入门学习教程

下一篇:利用Eclipse开发Hibernate应用程序