Saturday, December 6, 2014

Create Struts2 project with eclipse and maven

  • Create a Struts2 project with eclipse
  • Convert to maven project
  • First HelloStruts2 example

1. Create a Dynamic Web Project with eclipse Kepler:

Eclipse -> new project -> select Dynamic Web Project -> enter Project name (HelloStruts2) -> select Next -> select Generate web.xml and click finish.

Add struts.xml to folder /WEB-INF/classes. Create folder /classes if it does not existed.
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE struts PUBLIC  
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
   "http://struts.apache.org/dtds/struts-2.0.dtd">  
 <struts>  
      <constant name="struts.devMode" value="true" />  
 </struts>  

Add the filter StrutsPrepareAndExecuteFilter to web.xml as the below:
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  <display-name>HelloStruts2</display-name>  
  <display-name>Struts2 with eclipse and maven</display-name>  
      <welcome-file-list>  
           <welcome-file>pages/index.jsp</welcome-file>  
      </welcome-file-list>  
      <filter>  
           <filter-name>struts2</filter-name>  
           <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
      </filter>  
      <filter-mapping>  
           <filter-name>struts2</filter-name>  
           <url-pattern>/*</url-pattern>  
      </filter-mapping>  
 </web-app>  

2. Convert to maven project:

Right click on project -> Configure -> Convert to Maven Project.

Add dependency for struts2 in pom.xml:
 <dependency>   
         <groupId>org.apache.struts</groupId>   
         <artifactId>struts2-core</artifactId>   
         <version>2.3.16.3</version>   
    </dependency>   

3. First HelloStruts2 example

Create HelloAction.java

 package com.vmtram.struts2.action;  
 public class HelloAction {  
      private static final String SUCCESS = "SUCCESS";  
      private String username;  
      public String doAction() {  
           return SUCCESS;  
      }  
      // Getter & Setter  
      public String getUsername() {  
           return username;  
      }  
      public void setUsername(String username) {  
           this.username = username;  
      }  
 }  

Mapping the action in struts.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE struts PUBLIC  
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
   "http://struts.apache.org/dtds/struts-2.0.dtd">  
 <struts>  
      <constant name="struts.devMode" value="true" />  
      <package name="helloWorld" extends="struts-default">  
           <action name="hello" class="com.vmtram.struts2.action.HelloAction"  
                method="doAction">  
                <result name="SUCCESS">/pages/hello-view.jsp</result>  
           </action>  
      </package>  
 </struts>  

Create a view index.jsp to input data
 <%@ page language="java" contentType="text/html; charset=UTF-8"  
      pageEncoding="ISO-8859-1"%>  
 <%@ taglib prefix="s" uri="/struts-tags"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Index</title>  
 </head>  
 <body>  
      <s:form action="hello">  
           <s:textfield name="username" label="Enter your name" />  
           <s:submit value="SUBMIT" />  
      </s:form>  
 </body>  
 </html>  

Create a view hello-view.jsp to show data
 <%@ page language="java" contentType="text/html; charset=UTF-8"  
      pageEncoding="UTF-8"%>  
 <%@ taglib prefix="s" uri="/struts-tags"%>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
 <head>  
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
 <title>Hello view</title>  
 </head>  
 <body>  
      Hello  
      <s:property value="username" />  
 </body>  
 </html>  

The project structure:

The result:

Related:

No comments:

Post a Comment