1. Load the class library of struts2.
the class library contain following jar package:(1) struct2-core-xxx.jar -- Core class library
(2) xwork-core-xxx.jar -- xWork class library, basic of building struts2
(3) ognl-xxx.jar -- Language expression library
(4) freemarker-xxx.jar -- Class library for label template
(5) javassist-xxx.GA.jar -- Class library for dealing bytecode
(6) commons-fileupload-xxx.jar -- Class library for uploading files
(7) commons-io-xxx.jar -- Expansion library for Java IO
(8) commons-lang-xxx.jar -- Class library containing utility class with Data Type
2. Configure the file named "web.xml"
<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>
Reason: Position all requests to the specified filters
3. Develop pages in view layer
<s:property value="message"/>
<form action="helloWorld.action" method="post">
<input name="name" type="text"/>
<input type="submit" value="submit"/>
</form>
4. Develop Action in controller layer
public class HelloWorkAction implements Action {
private String name =""; // Get value from <input name="name" type="text"/>
private String message = ""; // Populate the content to <s:property value="message"/>
@override
public String execute() {
this.setMessage("Hello" + this.getName() + "!"); // set the content and got by <s:property value="message"/>
return "success"; // server-side turn page
}
... // omit setter and getter methods
}
5. Configure the file named "struts.xml"
<struts>
<package name="default" namespace="/" extends="struts-default">
<!-- contact form and class, form name is helloWorld.action -->
<action name="hellowWorld" class="cn.jbit.strutsdemo.HelloWorldAction">
<!-- return to "success" is the same as request.getRequestDispatcher("helloWorld.jsp").forward(request, response) -->
<result name="success">helloWorld.jsp</result>
</action>
</action>
</package>
</struts>
6. Deploy and run the program
Run with tomcat
------------------------Questions-------------------------------
1. What's the usage of package label in the file named "struts.xml"?
package actions and inherit or be inherited by other package
2. Where to place the file named "struts.xml"?
1. default path:
src/
2. configuring path in web.xml:
when configure struts filter, add following context in filter label.
<filter>
...
<init-param>
<param-name>config</param-name>
<param-value>../conf/struts.xml</param-value>
</init-param>
</filter>