由于公司的一个系统需要进行web化,对几种常见的web技术进行了调查。试用了下struts,理解了struts的开发流程。以下是试作的一个login的小例子。
发环境:jdk1.5.06 struts1.2.7 netbeans5.0(内嵌tomcat5.5.9)
首先,使用nb创建一个web工程:helo。选中是否使用struts1.2.7的复选框。
创建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; } }
创建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); } }
创建login.jsp文件:
lt;%@page contenttype="text/html; charset=gbk"%>
lt;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>
lt;/html>
修改struts-config.xml文件,添加以下的内容:
lt;form-beans>
lt;form-bean name = "adduseractionform" type = "com.myapp.struts.adduseractionform"/>
lt;form-bean name="loginactionform" type="com.myapp.struts.loginactionform" />
lt;/form-beans>
lt;global-forwards>
lt;forward name="welcome" path="/welcome.do"/>
<forward name="login" path="/login.jsp" />
lt;/global-forwards>
lt;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" />
lt;/action-mappings>
部署:
使用netbeans部署这个web服务,即可。
测试:
启动tomcat,在浏览器中输入http://localhost:8084/helo/login.jsp即可。
闽公网安备 35060202000074号