Friday, September 2, 2011

Struts2 MVN practice

1) use MVN archetype:generate to create struts blank project number,choose number 198,give a artifactId and GroupId,choose the struts2 version and package and other parameters

2) after project created,we can not clean,compile and package project because of folowing error,we need fix some reference in pom.xml
Reason: Resolving expression: '${project.version}': Detected the following recursive expression cycle: [] for project rogers:helloWorld at /home/dttdev1/struts2/helloWorld/pom.xml

3) add following line in properties tag,change struts2.Version to a real struts2 version like 2.2.3,
delet all spring dependencies,and test class and packages
<project.version>1.0.0.0</project.version>
4) after that,we can clean,compile,package and test it inside jetty by mvn jetty:run

5)create a newexamples.xml file in same folder as struts.xml reside.
6) newexamples.xml content:

<?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">
<!-- - This file is included by the struts.xml file as an example - of how
to break up the configuration file into multiple files. -->
<struts>
<package name="newexamples" namespace="/mytest" extends="struts-default">
<action name="Name">
<result>/newexamples/namecollect.jsp</result>
</action>
<action name="HelloName" class="rogers.newexamples.TestAction">
<result name="SUCCESS">/newexamples/hello.jsp</result>
</action>
</package>
</struts>

7) include this xml file in struts.xml

<include file="newexamples.xml" />
8) create action class:

package rogers.newexamples;

public class TestAction {
private String name;
private String greeting;

public String execute() {
setGreeting("hello, " + getName());
return "SUCCESS";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getGreeting() {
return greeting;
}

public void setGreeting(String greeting) {
this.greeting = greeting;
}

}

9) create two jsp file in newexamples folders(namecollect.jsp and hello.jsp

<s:form action="HelloName">
<s:textfield name="name" label="Your name" />
<s:submit />
</s:form>


<s:property value="greeting"/>

start jetty,then point to http://localhost:8080/tutorial/mytest/Name,it will display namecollect.jsp page,after give a name,click submit,it will redirect to a hello.jsp page with a greeting massage

thinking: 1)if we put newexamples.xml in different folder,even we include the xml file,but when we visit the namecollect page by action Name,it will complain that action name Name can not be find,not action mapped to name "Name".

we define action like this
<action name="Name">
<result>/newexamples/namecollect.jsp</result>
</action>
this means when we visit http://localhost:8080/tutorial/mytest/Name
it will show namecollect.jsp page
inside namecollect.jsp page we define

<s:form action="HelloName">
<s:textfield name="name" label="Your name" />
<s:submit />
</s:form>

that means when we give a name and click submit,textfield content will be send to mapped action HelloName(TestAction class)'s property name,then call it's execute method to setup property greeting.

from mapped action HelloName definition

<action name="HelloName" class="rogers.newexamples.TestAction">
<result name="SUCCESS">/newexamples/hello.jsp</result>
</action>
we know that when execute return "SUCCESS",it will carry the java bean's property greeting to page hello.jsp and render the hello.jsp to explorer.

last ,dont forget to add a extra line to in pom.xml for jetty configuration

<scanTarget>src/main/resources/newexamples.xml</scanTarget>

No comments:

Post a Comment