- Be able to run actions without having to alter the URL at runtime
- Provide XML insight for struts.xml
- View the struts.xml file from the navigator
- Include the struts 2 tags in the component palette
1.Create an empty project called "Web"
2. Add a HTML file called index.html, ignoring it's content for now.
3. This will give us a "public_html" directory and of course a web.xml , so at this point navigate to the file system and add the Struts 2 JAR files as follows, all these are required to "WEB-INF\lib".
- commons-fileupload-1.2.2.jar
- commons-logging-1.1.1.jar
- freemarker-2.3.16.jar
- javassist-3.11.0.GA.jar
- ognl-3.0.1.jar
- struts2-core-2.2.3.jar
- xwork-core-2.2.3.jar
5. Now to view struts.xml in the navigator we have to edit the project properties and add the "WEB-INF\classes" to the project source path so it shows in the navigator as shown below.
6. From the JAR file "struts2-core-2.2.3.jar" extract the DTD "struts-2.1.7.dtd" and place it into the "WEB-INF\classes". It's the DTD which will enable XML code insight to help us build our struts 2 XML config file.
7. Refresh the project to ensure your project is starting to take shape
8. Edit struts2.xml and add content as follows, you could use code insight if you wanted to now we have that setup. You will see we reference the DTD as a SYSTEM file itself from the same directory.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts SYSTEM "struts-2.1.7.dtd"> <struts> <package name="struts-demo" namespace="/" extends="struts-default"> <action name="entername"> <result>nameinput.jsp</result> </action> <action name="sayHello" class="pas.acs.struts.demo.actions.Hello"> <result name="input">nameinput.jsp</result> <result>response.jsp</result> </action> </package> </struts>
9. Create our action class as follows.
package pas.acs.struts.demo.actions; import com.opensymphony.xwork2.ActionSupport; public class Hello extends ActionSupport { private String name; private String message; public Hello() { } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setMessage(String message) { this.message = message; } public String getMessage() { return String.format("Hello %s", getName()); } }
10. Create a Hello-validation.xml to use for validation in the same directory as our class above.
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="name"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>Please enter name</message> </field-validator> </field> </validators>
11. Create the 2 required JSP as follows
nameinput.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page contentType="text/html;charset=windows-1252"%> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <style type="text/css"> .errorMessage { color:red; } </style> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/> <title>Struts 2 Demo In JDeveloper 11g PS3</title> </head> <body> <h2>Struts 2 Demo In Jdeveloper 11g PS3</h2> <s:form action="sayHello" method="POST"> <s:textfield label="Your Name" name="name" /> <s:submit value="Go" /> </s:form> </body> </html>
response.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page contentType="text/html;charset=windows-1252"%> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/> <title>Struts 2 Demo In JDeveloper 11g PS3</title> </head> <body> <h2>Struts 2 Demo In JDeveloper 11g PS3</h2> <s:property value="message" /> </body> </html>
12. Edit index.html with content as follows to start our struts 2 application with the correct URL syntax to invoke the action.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=entername.action"> </head> <body> <p>Loading ...</p> </body> </html>
13. Edit then project properties and select "JSP tag Libraries' and then select "Add" and navigate to "struts2-core-2.2.3.jar" to find the struts tags and add them to the component palette.
14. Edit web.xml to look as follows.
<?xml version = '1.0' encoding = 'windows-1252'?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
15. Run index.html to invoke the Integrated WebLogic server and verify a page as follows.
16. Enter in your name and press the "go" button
So now you have a Struts 2 starter project for JDeveloper 11g (11.1.1.5)
Your project would look as follows within JDeveloper 11g IDE