【IT168 技术文档】由于公司的一个系统需要进行WEB化,对几种常见的WEB技术进行了调查。试用了下Struts,理解了Struts的开发流程。以下是试作的一个Login的小例子。
开发环境:JDK1.5.06 Struts1.2.7 NetBeans5.0(内嵌Tomcat5.5.9)
1 首先,使用NB创建一个WEB工程:Helo。选中是否使用Struts1.2.7的复选框。
2 创建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 创建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 创建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文件,添加以下的内容:
<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部署这个Web服务,即可。
7 测试:
启动Tomcat,在浏览器中输入http://localhost:8084/Helo/login.jsp即可。