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.
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标注和习惯代替配置。)
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 Ajax tags
Multiple view options (JSP, Freemarker, Velocity and
XSLT)
Plug-ins to extend and modify framework features
The MVC/Struts2 Arichitecture
Figure 1 overlays the model, view and controller to the Struts2
high level architecture. The controller is implemented with a
Struts2 dispatch servlet filter as well as interceptors, the model is
implemented with actions, and the view as a combination of
result types and results. The value stack and OGNL provide
common thread, linking and enabling integration between the
other components.
The value stack is exactly what it says it is – a stack of objects. OGNL
stands for Object Graph Navigational Language, and provides
the unified way to access objects within the value stack.
实例代码分析(登陆例子):
Login.java:
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
private String name;
private String password;
private String message;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getpassword(){
return password;
}
public void setPassword(String password){
this.password=password;
}
public String getMessage(){
return message;
}
public void setMessage(String message){
this.message=message;
}
public String execute(){
if("admin".equals(name)&&"admin".equals(password)){
message="欢迎您"+name+"!";
return SUCCESS;
}else{
message="The name or password is wrong!";
return INPUT;
}
}
}
Struts.xml
<?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">
<struts>
<include file="struts-default.xml"/>
<package name="com.struts20" extends="struts-default" namespace="/">
<action name="Login" class="com.struts20.Login">
<result name="success">success.jsp</result>
<result name="input">login.jsp</result>
</action>
</package>
</struts>
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>struts2.0 test</display-name>
<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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
Login.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HelloChina</title>
</head>
<body>
<h3><s:property value="message" /></h3> //显示相应的提示信息。
<h3>请输入正确的用户名及密码:</h3>
<s:form action="/Login.action" method="POST">
<s:textfield name="name" label="用户名"/>
<s:password name="password" label="密码" />
<s:submit value="登陆"/>
</s:form>
</body>
</html>
注意:
1,Struts2.X所有标志都在URI为“/struts-tags”命名空间下,不过,我们可以从功能上将其分为两大类:非UI标志和UI标志.
2. SUCCESS在接口com.opensymphony.xwork2.Action中定义,另外同时定义的还有ERROR, INPUT, LOGIN, NONE。此外,我在配置Action时都没有为result定义名字(name),所以它们默认都为success。值得一提的是Struts 2.0中的result不仅仅是Struts 1.x中forward的别名,它可以实现除forward外的很激动人心的功能,如将Action输出到FreeMaker模板、Velocity模板、JasperReports和使用XSL转换等。这些都过result里的type(类型)属性(Attribute)定义的。
如:<action name="VMHelloWorld" class="tutorial.HelloWorld">
<result type="velocity">/HelloWorld.vm</result>
</action>
3. “%”符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值。
访问OGNL上下文和Action上下文,#相当于ActionContext.getContext();