Struts 2 Zero Configuration by convention

We can make struts 2 application without the design document struts.xml. There are two different ways to make zero design file:

  • By convention
  • By annotation

The ventures to make zero design document utilizing show is as follows:

  1. Create input page (optional)
  2. Create the activity class
  3. Create see components

Example to make zero setup record by convention.

In this model, we are making 4 pages :

  1. index.jsp
  3. login-success.jsp
  4. login-error.jsp

1) Create index.jsp for info (optional)

This jsp page makes a structure utilizing struts UI labels. It gets name and secret key from the client.

  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. <s:form action="login">  
  3. <s:textfield name="name" label="Name"></s:textfield>  
  4. <s:password name="password" label="Password"></s:password>  
  5. <s:submit value="login"></s:submit>  
  6. </s:form>  

2) Create the action class

The action class name must be suffixed by action following the request name or it must implement the Action interface (or extend ActionSupport). Suppose that request name is login, action class name should be LoginAction if you don't want to implement Action interface.

The action class must be located inside the action or actions or struts or struts2 package.
  1. package action;  
  2. public class LoginAction {  
  3. private String name,password;  
  4. public String getName() {  
  5.     return name;  
  6. }  
  7. public void setName(String name) {  
  8. = name;  
  9. }  
  11. public String getPassword() {  
  12.     return password;  
  13. }  
  14. public void setPassword(String password) {  
  15.     this.password = password;  
  16. }  
  17. public String execute(){  
  18.     if(password.equals("struts")){  
  19.         return "success";  
  20.     }  
  21.     else  
  22.         return "error";  
  23. }  
  24. }  

3) Create view components

The see view must be situated inside the WEB-INF/content folder.

The see parts must be named with solicitation name - (hyphen) string returned by activity class. Assume that demand name is login and string returned by activity class is achievement and blunder, document name must be login-success.jsp and login-error.jsp.

  1. <%@ taglib uri="/struts-tags" prefix="s" %>  
  2. Welcome,<s:property value="name"/>  
  1. Sorry username or password error!  
  2. <jsp:include page="/index.jsp"></jsp:include>  

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition