<o:p> </o:p>
Apache Struts 2 is an elegant, extensible framework for creating enterprise-ready Java web applications. The framework is designed to streamline the full development cycle, from building, to deploying, to maintaining applications over time.
Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.
<o:p> </o:p>
The goal of Struts2 is simple – to make web development easier
for the developer.To achieve this goal Struts2 provides features
to reduce XML configuration via intelligent defaults, utilizes
annotations and provides conventions over configuration.
(Struts2的目标——使用web开发更加简单为了实现,为了实现这个简单目标,
struts通过智能的默认值减少XML配置文件,利用java标注和习惯代替配置。)
<o:p> </o:p>
<o:p> </o:p>
Here are some of the features that may lead you to consider Struts2:
Action based framework
Mature with a vibrant developer and user community
Annotation and XML configuration options
POJO-based actions that are easy to test
Spring, SiteMesh and Tiles integration
OGNL expression language integration
Themes based tag libraries and <st1:city w:st="on"><st1:place w:st="on">Ajax</st1:place></st1:city> tags
Multiple view options (JSP, Freemarker, Velocity and
XSLT)
Plug-ins to extend and modify framework features
<o:p> </o:p>
The MVC/Struts2 Arichitecture
<o:p> <v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"> <v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id="_x0000_i1025" style="WIDTH: 414.75pt; HEIGHT: 244.5pt" type="#_x0000_t75"><v:imagedata o:title="" src="file:///C:\DOCUME~1\pengch\LOCALS~1\Temp\msohtml1\01\clip_image001.emz"></v:imagedata></v:shape></o:p>
<v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape id="_x0000_i1025" style="WIDTH: 414.75pt; HEIGHT: 244.5pt" type="#_x0000_t75"><v:imagedata o:title="" src="file:///C:\DOCUME~1\pengch\LOCALS~1\Temp\msohtml1\01\clip_image001.emz"></v:imagedata></v:shape>
Figure 1 overlays the model, view and controller to the Struts2<o:p></o:p>
high level architecture. The controller is implemented with a<o:p></o:p>
Struts2 dispatch servlet filter as well as interceptors, the model is<o:p></o:p>
implemented with actions, and the view as a combination of<o:p></o:p>
result types and results. The value stack and OGNL provide<o:p></o:p>
common thread, linking and enabling integration between the<o:p></o:p>
other components.<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
The value stack is exactly what it says it is – a stack of objects. OGNL<o:p></o:p>
stands for Object Graph Navigational Language, and provides<o:p></o:p>
the unified way to access objects within the value stack.<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
实例代码分析(登陆例子):<o:p></o:p>
Login.java:<o:p></o:p>
import com.opensymphony.xwork2.ActionSupport;<o:p></o:p>
public class Login extends ActionSupport {<o:p></o:p>
private String name;<o:p></o:p>
private String password;<o:p></o:p>
private String message;<o:p></o:p>
public String getName(){<o:p></o:p>
return name;<o:p></o:p>
}<o:p></o:p>
public void setName(String name){<o:p></o:p>
this.name=name;<o:p></o:p>
}<o:p></o:p>
public String getpassword(){<o:p></o:p>
return password;<o:p></o:p>
}<o:p></o:p>
public void setPassword(String password){<o:p></o:p>
this.password=password;<o:p></o:p>
}<o:p></o:p>
public String getMessage(){<o:p></o:p>
return message;<o:p></o:p>
}<o:p></o:p>
public void setMessage(String message){<o:p></o:p>
this.message=message;<o:p></o:p>
}<o:p></o:p>
public String execute(){<o:p></o:p>
if("admin".equals(name)&&"admin".equals(password)){<o:p></o:p>
message="欢迎您"+name+"!";<o:p></o:p>
return SUCCESS;<o:p></o:p>
}else{<o:p></o:p>
message="The name or password is wrong!";<o:p></o:p>
return INPUT;<o:p></o:p>
} <o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
Struts.xml<o:p></o:p>
<?xml version="1.0" encoding="UTF-8"?><o:p></o:p>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"<o:p></o:p>
"http://struts.apache.org/dtds/struts-2.0.dtd"><o:p></o:p>
<struts><o:p></o:p>
<include file="struts-default.xml"/><o:p></o:p>
<package name="com.struts20" extends="struts-default" namespace="/"><o:p></o:p>
<o:p></o:p>
<action name="Login" class="com.struts20.Login"><o:p></o:p>
<result name="success">success.jsp</result><o:p></o:p>
<result name="input">login.jsp</result><o:p></o:p>
</action><o:p></o:p>
<o:p></o:p>
</package><o:p></o:p>
</struts><o:p></o:p>
Web.xml:<o:p></o:p>
<?xml version="1.0" encoding="UTF-8"?><o:p></o:p>
<web-app version="2.4" <o:p></o:p>
xmlns="http://java.sun.com/xml/ns/j2ee" <o:p></o:p>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <o:p></o:p>
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee <o:p></o:p>
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><o:p></o:p>
<display-name>struts2.0 test</display-name><o:p></o:p>
<filter><o:p></o:p>
<filter-name>struts2</filter-name><o:p></o:p>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class><o:p></o:p>
</filter><o:p></o:p>
<filter-mapping><o:p></o:p>
<filter-name>struts2</filter-name><o:p></o:p>
<url-pattern>/*</url-pattern><o:p></o:p>
</filter-mapping><o:p></o:p>
<welcome-file-list><o:p></