JBuilder 开发 Struts实例

来源:百度文库 编辑:神马文学网 时间:2024/04/29 23:06:26
一、JBuilder 对于 Struts 之支持
JBuilder9.0(2006TM)增加了对struts框架的支持功能,使用JBuilder9.0(2006TM)你可以免去手工搭建开发环境的许多过程。要在JBuilder9.0(2006TM)采用struts框架,只需创建web module(WAR)选择struts 1.2,并设置jsp/servlet为tomcat 5.5即可。
Create empty web module命名为:loginstruts
完成以上过程之后,向导将自动完成以下任务:
1)将struts.jar文件复制到web应用程序的/WEB-INF/lib目录下。
2)将struts-bean.tld(Target Library Descriptor:标签库描述文件)、struts-html.tld、struts-template.tld、struts-logic.tld、struts-tile.tld和struts-nested.tld等复制到/WEB-INF目录下。
3)产生struts-config.xml文件并自动编写成最小化配置
4)自动配置web.xml文件。
二、使用Jbuilder开发struts实例
下面我们来创建一个简单的登录验证程序,说明struts-config编辑器和ActionForm,Action向导的用法。借机来帮助大家了解Struts Framework。
(1)首先使用actionform向导来创建一个LoginForm.
package loginstruts;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
public class LoginForm extends ActionForm {
private String password;
private String username;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** @todo: finish this method, this is just the skeleton.*/
ActionErrors errors=new ActionErrors();
if(username==null||username.equals("")){
errors.add("username",new ActionError("error.username"));
}
return errors;
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
password = null;
username = null;
}
}
(2)接下来,使用action向导创建一个LoginAction类,并将FormBean Name属性值设置成上一步完成的LogonForm.
package loginstruts;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.*;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
ActionErrors errors = new ActionErrors();
if (loginForm.getUsername().equals("zhutao") && loginForm.getPassword().equals("zhutao")) {
return mapping.findForward("success");
} else if(loginForm.getUsername().equals("zhutao")) {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("app.passworderror"));
} else {
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("app.usernameerror"));
}
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("failure");
}
}
(3)在struts应用中,为了使程序更好的地国际化,通常将所有的字符消息编写在一个后缀为.properties的资源文件中。这个文件保存在project source/loginstruts/ApplicationResources.properties。在这里添加键值,以备后面程序之需。
app.title=helloapp
app.username=User Name
app.password=Password
app.hello=Welcome
app.usernameerror=Your username is invalid
app.passworderror=Your password is invalid
app.loginagain=Login Again
error.username=Please input username
同时注意在web.xml文件中添加以下配置语句:

application
ApplicationResources

(4)配置好struts-config.xml文件如下:




















(5)web.xml文件:


loginapp

3



/WEB-INF/struts-bean.tld
/WEB-INF/struts-bean.tld


/WEB-INF/struts-html.tld
/WEB-INF/struts-html.tld


/WEB-INF/struts-logic.tld
/WEB-INF/struts-logic.tld


/WEB-INF/struts-tiles.tld
/WEB-INF/struts-tiles.tld


/WEB-INF/struts-nested.tld
/WEB-INF/struts-nested.tld



action
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml


application
ApplicationResources


debug
2

2


action
*.do


Added by JBuilder to compile JSPs with debug info
debugjsp
org.apache.jasper.servlet.JspServlet

classdebuginfo
true

3


debugjsp
*.jsp


(6)最后编写login.jsp,login_error.jsp,hello.jsp页面。
login.jsp
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ page contentType="text/html; charset=windows-1252" %>


<br><bean:message key="app.title"/><br>




Logon app using struts (MVC)
















login_error.jsp
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>


<br><bean:message key="app.title"/><br>



Logon error






hello.jsp
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>


<br>hello<br>



logon succ





为了方便应用程序直接进入login.jsp页面,可以在web.xml文件中加如下配置语句:

login.jsp

启动web服务器,在浏览器中输入http://localhost:8083/loginapp/login.jsp即可进入登录界面。