Вы находитесь на странице: 1из 8

This article explains how you can setup quickly an application based on Struts on JBoss

5 application server.

Install JBoss Struts plugin on Eclipse


For our example we have installed Eclipse IDE for Java Developers (Ganymede) which
is the Enterprise Version of Eclipse foundation: check it here:
http://www.eclipse.org/downloads/

Once you have extracted Eclipse you need to update your plugins: from the Help
Menu, select "Software Updates". In the "Available Software" Click on "Add site" and
add JBoss plugins site:
http://download.jboss.org/jbosstools/updates/nightly/trunk/
Here there's a whole lot of plugins, however what you need is basically JBoss AS plugin
and Struts plugin. Once installed you have to restart Eclipse.
Create the Web project
Create a new Dynamic Web Project from Menu : File | New | Other | Web |
Dynamic Web project.

Set the Web module version to 2.5 and choose as Runtime your jBoss Runtime
environment.

Now Add Struts capabilities to your project. Right click on it and select:
The Struts wizard will start. The first gui will ask where you have your web.xml file.
You can leave the default value.

Next GUI, Project Modules, will add Struts Support to your Project. Click on the
Button and select the Struts version and configuation files.

Now your project Modules should be compiled with the correct configuration. Click
next.

In the last applet you will basically select the classes/lib path Servlet and Struts
version. Select Servlet version 2.5 , Struts 1.2 and leave unchecked the "Add
libraries".
Unfortunately I found some incompatibilities with the libraries provided by the Tool so
we have to donwload the libraries from Jakarta site.

Download Struts libraries


Download Struts 1.3 libraries from jakarta site:
http://apache.fastbull.org/struts/library/struts-1.3.10-lib.zip

Unzip the libraries in a folder, such as StrutsLibs

Configure the Project to use Struts libraries


Configuring your project to use Struts libraries by right-clicking on the Project and
selecting "Properties". Configure the build-path by adding all libraries except
commons-logging.jar which is already loaded by JBoss.
The libraries you have selected needs to be added also in the WEB-INF/lib folder. You
can do this by selecting again Project properties | Java EE Module Dependancies.
Check all Struts libraries and Click Ok.

Add Struts Components


Following is a sample Struts application which we have used to test Struts on JBoss.
The configuration file struts-config.xml contains basically one Action named
com.sample.LoginAction , one FormBean named com.sample.LoginFormBean
and three jsp: login.jsp, welcome.jsp and error.jsp.
view plaincopy to clipboardprint?
1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 1.2//EN"
3. "http://struts.apache.org/dtds/struts-config_1_2.dtd">
4. <struts-config>
5. <data-sources/>
6. <form-beans>
7. <form-bean name="LoginForm" type="com.sample.LoginFormBean"/>
8. </form-beans>
9. <global-exceptions/>
10. <global-forwards/>
11. <action-mappings>
12. <action input="/login.jsp" name="LoginForm" path="/checkLogin"
13. scope="request" type="com.sample.LoginAction">
14. <forward name="welcome" path="/welcome.jsp"/>
15. <forward name="error" path="/error.jsp"/>
16. </action>
17. </action-mappings>
18. <controller/>
19. <message-resources parameter="com.sample.ApplicationResources"/>
20. </struts-config>
This is how our configuration looks like from the Diagram view:

This is our login.jsp page:

view plaincopy to clipboardprint?


1. <%@ taglib uri="/WEB-INF/struts-html" prefix="html"%>
2.
3. <html:html>
4.
5. <body>
6. <html:form action="/checkLogin">
7. <html:errors />
8.
9. <table>
10. <tr>
11. <td align="center" colspan="2"><font size="4">Please
12. Login</font>
13. </tr>
14. <tr>
15. <td align="right">Username</td>
16. <td align="left"><html:text property="username" size="30"
17. maxlength="30" /></td>
18. </tr>
19. <tr>
20. <td align="right">Password</td>
21. <td align="left"><html:text property="password" size="30"
22. maxlength="30" /></td>
23. </tr>
24.
25. <tr>
26. <td align="right"><html:submit>Login</html:submit></td>
27. <td align="left"><html:cancel>Cancel</html:cancel></td>
28. </tr>
29. </table>
30. </html:form>
31.
32.
33. </body>
34. </html:html>
The login.jsp is backed by a LoginFormBean which contains the mapping for the
corresponding fields:

view plaincopy to clipboardprint?


1. package com.sample;
2.
3. import javax.servlet.http.HttpServletRequest;
4.
5. import org.apache.struts.action.ActionErrors;
6. import org.apache.struts.action.ActionMapping;
7. import org.apache.struts.action.ActionMessage;
8.
9. public class LoginFormBean extends org.apache.struts.action.ActionForm {
10.
11. String username;
12. String password;
13.
14. public String getUsername() {
15. return username;
16. }
17.
18. public void setUsername(String username) {
19. this.username = username;
20. }
21.
22. public String getPassword() {
23. return password;
24. }
25.
26. public void setPassword(String password) {
27. this.password = password;
28. }
29.
30. public LoginFormBean () {
31. }
32.
33. public void reset(ActionMapping actionMapping, HttpServletRequest request)
{
34. // TODO: Write method body
35. this.username = null;
36. this.password = null;
37. }
38.
39. public ActionErrors validate(ActionMapping actionMapping, HttpServletReque
st request) {
40. ActionErrors errors = new ActionErrors();
41.
42. if( getUsername() == null || getUsername().length() < 1 ) {
43. errors.add("username",new ActionMessage("username.error"));
44. }
45. if( getPassword() == null || getPassword().length() < 1 ) {
46. errors.add("password",new ActionMessage("password.error"));
47. }
48. return errors;
49.
50. }
51.
52.
53. }
Notice this class uses a Validator to check the validity of the fields. You have to add a
file named ApplicationResources.properties in the folder com.sample

username.error=username cannot be null


password.error=Password cannot be null

Last piece of code is the LogicAction which demandates the logic for logging in to an
external DAO class....(which you have to implement)

view plaincopy to clipboardprint?


1. package com.sample;
2.
3. import javax.servlet.http.HttpServletRequest;
4. import javax.servlet.http.HttpServletResponse;
5.
6. import org.apache.struts.action.ActionForm;
7. import org.apache.struts.action.ActionForward;
8. import org.apache.struts.action.ActionMapping;
9.
10.
11. import com.sample.LoginDAO;
12. import com.sample.LoginFormBean;
13.
14. public class LoginAction extends org.apache.struts.action.Action {
15.
16. public LoginAction() {
17. }
18.
19. public ActionForward execute(ActionMapping mapping, ActionForm form, Htt
pServletRequest request, HttpServletResponse response) throws Exception {
20. LoginFormBean loginForm = (LoginFormBean)form;
21. boolean validLogin = LoginDAO.login(loginForm.getUsername(),
22. loginForm.getPassword());
23.
24. if (validLogin) {
25. return mapping.findForward("welcome");
26. }
27. else {
28. return mapping.findForward("error");
29. }
30. }
31.
32. }
That's all. Add two landing pages welcome.jsp and error.jsp and deploy your
application. If you have deployed your application as StrutsExample.war then you can
access to it with: http://localhost:8080/StrutsExample/login.jsp

Вам также может понравиться