服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

在netbeans ide下开发struts应用

由于公司的一个系统需要进行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">

&nbsp;&nbsp;

<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即可。

扫描关注微信公众号