NetBeans Struts應(yīng)用實(shí)例詳解
由于公司的一個(gè)系統(tǒng)需要進(jìn)行WEB化,對(duì)幾種常見的WEB技術(shù)進(jìn)行了調(diào)查。試用了下NetBeans Struts,理解了NetBeans Struts的開發(fā)流程。以下是試作的一個(gè)Login的小例子。
開發(fā)環(huán)境:JDK1.5.06 Struts1.2.7 NetBeans5.0(內(nèi)嵌Tomcat5.5.9)
1 首先,使用NB創(chuàng)建一個(gè)WEB工程:Hello。選中是否使用Struts1.2.7的復(fù)選框。
2 創(chuàng)建LoginActionForm.java文件:
- public class LoginActionForm extends ActionForm ...{
- private String userName;
- private String userPwd;
- public String getUserName() ...{
- return userName;
- }
- public void setUserName(String userName) ...{
- this.userName = userName;
- }
- public void setUserPwd(String userPwd) ...{
- this.userPwd = userPwd;
- }
- public String getUserPwd() ...{
- return userPwd;
- }
- }
3 創(chuàng)建LoginAction.java文件:
- public class LoginAction extends Action ...{
- public ActionForward execute(ActionMapping mapping,
- ActionForm form,
- HttpServletRequest request,
- HttpServletResponse response) ...{
- LoginActionForm loginForm = (LoginActionForm) form;
- String forword="success";
- System.out.println("Name:" + loginForm.getUserName());
- System.out.println("Passwd:" + loginForm.getUserPwd());
- return mapping.findForward(forword);
- }
- }
4 創(chuàng)建Login.jsp文件:
- <%@page contentType="text/html; charset=GBK"%>
- <html>
- <head>
- <title>login</title>
- </head>
- <body bgcolor="#ffffff">
- <center>
- <h1>Welcome login into the system</h1>
- <form name="loginForm" method="post" action="loginAction.do">
- <br>
- <br>
- <table align="center">
- <tr>
- <td>UserName</td>
- <td>
- <input type="text" name="userName"/>
- </td>
- </tr>
- <tr>
- <td>Password</td>
- <td>
- <input type="password" name="userPwd"/>
- </td>
- </tr>
- <tr>
- <td> </td>
- <td>
- <input type="submit" name="Submit" value="Login">
-
- <input type="reset" value="Reset">
- </td>
- </tr>
- </table>
- </form>
- </center>
- </body>
- </html>
5 修改struts-config.xml文件,添加以下的內(nèi)容:
- <form-beans>
- <form-bean name = "AddUserActionForm" type = "com.myapp.struts.AddUserActionForm"/>
- <form-bean name="loginActionForm" type="com.myapp.struts.LoginActionForm" />
- </form-beans>
- <global-forwards>
- <forward name="welcome" path="/Welcome.do"/>
- <forward name="login" path="/login.jsp" />
- </global-forwards>
- <action-mappings>
- <action path="/Welcome" forward="/welcomeStruts.jsp"/>
- <action input="/login.jsp" name="loginActionForm" path="/loginAction" scope="request" type = "com.myapp.struts.LoginAction" validate="true" />
- </action-mappings>
6 部署:
使用NetBeans部署這個(gè)Web服務(wù),即可。
7 測(cè)試:
啟動(dòng)Tomcat,在瀏覽器中輸入http://localhost:8084/Helo/login.jsp即可。
【編輯推薦】