= Struts :jbake-date: 2016-09-06 :jbake-type: page :jbake-tomeepdf: :jbake-status: published Example struts can be browsed at https://github.com/apache/tomee/tree/master/examples/struts *Help us document this example! Click the blue pencil icon in the upper right to edit this page.* == AddUser [source,java] ---- package org.superbiz.struts; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties; public class AddUser { private int id; private String firstName; private String lastName; private String errorMessage; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getErrorMessage() { return errorMessage; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String execute() { try { UserService service = null; Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory"); Context ctx = new InitialContext(props); service = (UserService) ctx.lookup("UserServiceImplLocal"); service.add(new User(id, firstName, lastName)); } catch (Exception e) { this.errorMessage = e.getMessage(); return "failure"; } return "success"; } } ---- == AddUserForm [source,java] ---- package org.superbiz.struts; import com.opensymphony.xwork2.ActionSupport; public class AddUserForm extends ActionSupport { } ---- == FindUser [source,java] ---- package org.superbiz.struts; import javax.naming.Context; import javax.naming.InitialContext; import java.util.Properties; public class FindUser { private int id; private String errorMessage; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getErrorMessage() { return errorMessage; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String execute() { try { UserService service = null; Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory"); Context ctx = new InitialContext(props); service = (UserService) ctx.lookup("UserServiceImplLocal"); this.user = service.find(id); } catch (Exception e) { this.errorMessage = e.getMessage(); return "failure"; } return "success"; } } ---- == FindUserForm [source,java] ---- package org.superbiz.struts; import com.opensymphony.xwork2.ActionSupport; public class FindUserForm extends ActionSupport { } ---- == ListAllUsers [source,java] ---- package org.superbiz.struts; import javax.naming.Context; import javax.naming.InitialContext; import java.util.List; import java.util.Properties; public class ListAllUsers { private int id; private String errorMessage; private List users; public List getUsers() { return users; } public void setUsers(List users) { this.users = users; } public String getErrorMessage() { return errorMessage; } public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String execute() { try { UserService service = null; Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory"); Context ctx = new InitialContext(props); service = (UserService) ctx.lookup("UserServiceImplLocal"); this.users = service.findAll(); } catch (Exception e) { this.errorMessage = e.getMessage(); return "failure"; } return "success"; } } ---- == User [source,java] ---- package org.superbiz.struts; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; @Entity @Table(name = "USER") public class User implements Serializable { private long id; private String firstName; private String lastName; public User(long id, String firstName, String lastName) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; } public User() { } @Id public long getId() { return id; } public void setId(long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } ---- == UserService [source,java] ---- package org.superbiz.struts; import java.util.List; public interface UserService { public void add(User user); public User find(int id); public List findAll(); } ---- == UserServiceImpl [source,java] ---- package org.superbiz.struts; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.List; @Stateless public class UserServiceImpl implements UserService { @PersistenceContext(unitName = "user") private EntityManager manager; public void add(User user) { manager.persist(user); } public User find(int id) { return manager.find(User.class, id); } public List findAll() { return manager.createQuery("select u from User u").getResultList(); } } ---- == persistence.xml [source,xml] ---- --> ## struts.xml /addUserForm.jsp /addedUser.jsp /addUserForm.jsp /findUserForm.jsp /displayUser.jsp /findUserForm.jsp /displayUsers.jsp ## decorators.xml /* ## web.xml Learn EJB3 and Struts2 struts2 org.apache.struts2.dispatcher.FilterDispatcher actionPackages com.lq struts-cleanup org.apache.struts2.dispatcher.ActionContextCleanUp sitemesh com.opensymphony.module.sitemesh.filter.PageFilter struts-cleanup /* sitemesh /* struts2 /* index.jsp JSP configuration of all the JSP's *.jsp /prelude.jspf