1, web.xml
struts2的核心控制器FilterDispatcher被设计成一个Filter而不是一个普通servlet,为了让WEB应用加载FilterDispatcher,只须在web.xml配置FilterDispatcher。
2. struts.xml
新建struts.xml放入src路径下,struts已经为我们配置了一个默认的配置,具体可以查看核心包下的default.xml
3. ACTION
struts2的ACTION,不需要的继承类它就是一个简单的POJO,藕合度非常小,在类中默认的调用方法execute(),你也可以在struts.xml文件中配置默认调用方法。
4. VIEW
视图方面,struts2可以支持多样化,例如JSP,freemarker等等
下面是一个简单的例子:
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!-- 配置struts2 框架的核心Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!-- 配置struts2 filter拦截URL -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 如果Web应用使用servlet2.3以前的规范,
web不会自动加载strtus2框架的标签文件,须在web.xml中加载标签库,
另须在WEB-INF路径下手动加载struts-tags.tld -->
<!--<taglib>
<taglib-uri>/s</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib> -->
</web-app>
注:struts2.3及以上版本核心过滤器须改为 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
ACTION
package com.gsmfan.struts2.action;
public class UserAction {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String userAdd(){
//具体实现
return "success";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="false"></constant>
<package name="usermoudle" extends="struts-default">
<action name="user" class="com.gsmfan.struts2.action.UserAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
userAdd.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<s:form action="user!userAdd" method="post" theme="simple">
username:<s:textfield id="username" name="username"></s:textfield>
password:<s:password id="password" name="password"></s:password>
<s:submit value="ADD"></s:submit>
</s:form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
username:<s:property value="username"/>
password:<s:property value="password"/>
</body>
</html>