diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82ae7fd --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.iml +target +classes +build +.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..283249e --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Struts Example + +## Build the app with maven + +``` +$ mvn clean package +``` + +## Deploy the war to PCF + +``` +$ cf push struts-example -p target/struts.war +``` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..acf68c7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,89 @@ + + + + 4.0.0 + + org.superbiz.struts + struts + war + 1.1.0-SNAPSHOT + OpenEJB :: Web Examples :: Struts + http://tomee.apache.org + + + UTF-8 + + + + + apache-m2-snapshot + Apache Snapshot Repository + https://repository.apache.org/content/groups/snapshots + + true + + + + + + struts + + + + + org.apache.tomee + javaee-api + 7.0 + provided + + + javax.servlet + jstl + 1.2 + + + taglibs + standard + 1.1.2 + + + + mysql + mysql-connector-java + 5.1.13 + + + + org.apache.struts + struts2-core + 2.1.8.1 + + + org.apache.struts + struts2-sitemesh-plugin + 2.1.8.1 + + + + junit + junit + 4.12 + test + + + diff --git a/src/main/java/org/superbiz/struts/AddUser.java b/src/main/java/org/superbiz/struts/AddUser.java new file mode 100644 index 0000000..8fcd84a --- /dev/null +++ b/src/main/java/org/superbiz/struts/AddUser.java @@ -0,0 +1,80 @@ +/* + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +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"; + } +} diff --git a/src/main/java/org/superbiz/struts/AddUserForm.java b/src/main/java/org/superbiz/struts/AddUserForm.java new file mode 100644 index 0000000..e132005 --- /dev/null +++ b/src/main/java/org/superbiz/struts/AddUserForm.java @@ -0,0 +1,24 @@ +/* + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package org.superbiz.struts; + +import com.opensymphony.xwork2.ActionSupport; + +public class AddUserForm extends ActionSupport { + +} diff --git a/src/main/java/org/superbiz/struts/FindUser.java b/src/main/java/org/superbiz/struts/FindUser.java new file mode 100644 index 0000000..21ce444 --- /dev/null +++ b/src/main/java/org/superbiz/struts/FindUser.java @@ -0,0 +1,71 @@ +/* + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +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"; + } +} diff --git a/src/main/java/org/superbiz/struts/FindUserForm.java b/src/main/java/org/superbiz/struts/FindUserForm.java new file mode 100644 index 0000000..9f5eb06 --- /dev/null +++ b/src/main/java/org/superbiz/struts/FindUserForm.java @@ -0,0 +1,24 @@ +/* + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package org.superbiz.struts; + +import com.opensymphony.xwork2.ActionSupport; + +public class FindUserForm extends ActionSupport { + +} diff --git a/src/main/java/org/superbiz/struts/ListAllUsers.java b/src/main/java/org/superbiz/struts/ListAllUsers.java new file mode 100644 index 0000000..e59cffe --- /dev/null +++ b/src/main/java/org/superbiz/struts/ListAllUsers.java @@ -0,0 +1,72 @@ +/* + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +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"; + } +} diff --git a/src/main/java/org/superbiz/struts/User.java b/src/main/java/org/superbiz/struts/User.java new file mode 100644 index 0000000..c15bcbd --- /dev/null +++ b/src/main/java/org/superbiz/struts/User.java @@ -0,0 +1,68 @@ +/* + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +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; + } + +} diff --git a/src/main/java/org/superbiz/struts/UserService.java b/src/main/java/org/superbiz/struts/UserService.java new file mode 100644 index 0000000..784aaa5 --- /dev/null +++ b/src/main/java/org/superbiz/struts/UserService.java @@ -0,0 +1,29 @@ +/* + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package org.superbiz.struts; + +import java.util.List; + +public interface UserService { + + public void add(User user); + + public User find(int id); + + public List findAll(); +} diff --git a/src/main/java/org/superbiz/struts/UserServiceImpl.java b/src/main/java/org/superbiz/struts/UserServiceImpl.java new file mode 100644 index 0000000..1cd1d6d --- /dev/null +++ b/src/main/java/org/superbiz/struts/UserServiceImpl.java @@ -0,0 +1,43 @@ +/* + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +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(); + } + +} diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..079e3f3 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,65 @@ + + + + + + org.apache.openjpa.persistence.PersistenceProviderImpl + java:openejb/Resource/My DataSource + java:openejb/Resource/My Unmanaged DataSource + org.superbiz.struts.User + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/struts.xml b/src/main/resources/struts.xml new file mode 100644 index 0000000..ed85904 --- /dev/null +++ b/src/main/resources/struts.xml @@ -0,0 +1,44 @@ + + + + + + + + + /addUserForm.jsp + + + /addedUser.jsp + /addUserForm.jsp + + + /findUserForm.jsp + + + /displayUser.jsp + /findUserForm.jsp + + + /displayUsers.jsp + + + + diff --git a/src/main/webapp/WEB-INF/decorators.xml b/src/main/webapp/WEB-INF/decorators.xml new file mode 100644 index 0000000..6e95333 --- /dev/null +++ b/src/main/webapp/WEB-INF/decorators.xml @@ -0,0 +1,22 @@ + + + + + /* + + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..1328640 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,60 @@ + + + + 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 + + + diff --git a/src/main/webapp/addUserForm.jsp b/src/main/webapp/addUserForm.jsp new file mode 100644 index 0000000..b94309b --- /dev/null +++ b/src/main/webapp/addUserForm.jsp @@ -0,0 +1,23 @@ + +Add User + + + + + + diff --git a/src/main/webapp/addedUser.jsp b/src/main/webapp/addedUser.jsp new file mode 100644 index 0000000..2260bbd --- /dev/null +++ b/src/main/webapp/addedUser.jsp @@ -0,0 +1,19 @@ + +User added + +

User added successfully

diff --git a/src/main/webapp/decorators/layout.jsp b/src/main/webapp/decorators/layout.jsp new file mode 100644 index 0000000..6a18b66 --- /dev/null +++ b/src/main/webapp/decorators/layout.jsp @@ -0,0 +1,43 @@ + +<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" + prefix="decorator" %> +<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" + prefix="pages" %> + + + + + My Site - <decorator:title default="Welcome!"/> + + + + +
+ +
+

+ + +
+ +
+ + diff --git a/src/main/webapp/displayUser.jsp b/src/main/webapp/displayUser.jsp new file mode 100644 index 0000000..854bb3d --- /dev/null +++ b/src/main/webapp/displayUser.jsp @@ -0,0 +1,33 @@ + +User Details + +

User Details

+ + + + + + + + + + + + + +
ID${user.id }
First Name${user.firstName }
Last Name${user.lastName }
diff --git a/src/main/webapp/displayUsers.jsp b/src/main/webapp/displayUsers.jsp new file mode 100644 index 0000000..89a6edf --- /dev/null +++ b/src/main/webapp/displayUsers.jsp @@ -0,0 +1,32 @@ + +All Users +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + + + + + + + + + + +
IDFirst NameLast Name
${u.id }${u.firstName }${u.lastName }
diff --git a/src/main/webapp/findUserForm.jsp b/src/main/webapp/findUserForm.jsp new file mode 100644 index 0000000..4a2eb17 --- /dev/null +++ b/src/main/webapp/findUserForm.jsp @@ -0,0 +1,21 @@ + +Find User + + + + diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..4be7b0c --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,17 @@ + +Main page diff --git a/src/main/webapp/prelude.jspf b/src/main/webapp/prelude.jspf new file mode 100644 index 0000000..6d589b7 --- /dev/null +++ b/src/main/webapp/prelude.jspf @@ -0,0 +1,20 @@ + +<%@ taglib prefix="s" uri="/struts-tags" %> + diff --git a/src/main/webapp/style/layout.css b/src/main/webapp/style/layout.css new file mode 100644 index 0000000..66fb1c6 --- /dev/null +++ b/src/main/webapp/style/layout.css @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +body { + margin: 0; + padding: 0; + background: white; + text-align: center; +} + +div#page { + width: 800px; + margin: 0 auto; + padding: 0; + background: white; + text-align: center; +} + +div#header { + margin: 0 0 5em 0; + padding: 40px 20px; + color: white; + background: black; + text-align: center; +} + +div#content { + margin: 0; + padding: 0; +} + +div#footer { + color: white; + background-color: black; +} + +a, a:link, a:visited, a:active { + color: white; + background-color: black; +} + +a:hover { + text-decoration: underline; + font-size: larger; +} \ No newline at end of file