Article

Complete the MVC Puzzle with Struts

Page: 1 2 3 4 5 6 7 Next

The model for our application is a standard JavaBean object with no persistence, and is shown below:

package com.samjdalton.struts;      
     
     
public class LoginBean {      
   private String username;      
   private String password;      
     
   public void setUsername(String username) {      
       this.username=username;      
   }      
     
   public String getUsername() {      
       return username;      
   }      
     
   public void setPassword(String password) {      
       this.password = password;      
   }      
     
   public String getPassword() {      
       return password;      
   }      
}

Having seen the code elements of the application, we can look at the struts-config.xml for the application:

<?xml version="1.0" encoding="ISO-8859-1" ?>      
<!DOCTYPE struts-config PUBLIC      
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"      
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">      
<struts-config>      
   <form-beans>      
       <form-bean      
           name="loginForm"      
           type="com.samjdalton.struts.LoginForm"/>      
   </form-beans>      
     
   <action-mappings>      
     
       <action      
           path="/Login"      
           forward="/login.jsp"/>      
       <action      
           path="/Welcome"      
           forward="/welcome.jsp"      
           name="loginForm"      
           scope="request"/>      
     
       <action      
           path="/ProcessLogin"      
           type="com.samjdalton.struts.LoginAction"      
           name="loginForm"      
           scope="request"      
           validate="true"      
           input="/Login.do">      
           <forward      
               name="success"      
               path="/Welcome.do"/>      
           <forward      
               name="failure"      
               path="/Login.do"/>      
       </action>      
   </action-mappings>      
   <message-resources      
       parameter="ApplicationResources"      
       null="false" />      
</struts-config>

Most of this file is identical to the example shown earlier, the only addition is that of a <message-resources> tag. This tag allows us to externalize string type resources from the application code. This allows us very easily to internationalise our application. In the above example, the resources are contained in a file named ApplicationResources.properties, which must exist on the applications classpath (the easiest way to achieve this is to place it in the root of your WEB-INF/classes folder).

In order to deploy the application, see the Installing Struts section of this article.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links