Structs2
本文所用Structs2jar包版本struts-2.3.16.3,目前最新版。打包下载:
http://download.youkuaiyun.com/download/wang_haifeng/8622337
Structs2所需jar包最新下载地址:http://archive.apache.org/dist/struts/binaries/
本篇是入门篇。
Structs2类似webWork,是structs1和webWork的整合,这里简介大家就随便找篇文章看看,我就不说了。
下面开始搭建Structs2的小Demo:
目录结构如图,说明一下:structs.xml放在src下,习惯如图(src编译会到WEB-INF/classes下)。
- 导入jar包
下载解压里面的blank包,里面的lib下jar包是最小的需求包。全部拷贝到项目lib。
- web.xml
项目首先执行web.xml,所以我们先配下web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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</display-name>
<!-- 会去找structs.xml中的namespace -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- http://localhost:8080/Mis/后面所有的请求都交给struts2,建议不改 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/pages/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- structs.xml
配置structs.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>
<!-- 常量,设置为配置更改即重新加载,避免了更改action名造成重启服务器的问题 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- namespace为访问路径后的命名空间,为/代表http://localhost:8080/Mis/ -->
<package name="default" namespace="/" extends="struts-default">
<!-- 在命名空间下 访问的action名称,如http://localhost:8080/Mis/login-->
<action name="login" class="com.wanghaifeng.mis.action.LoginAction" method="login">
<!-- name对应action中return的字符串 -->
<result name="success">/WEB-INF/pages/welcome.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
- action
action类
package com.wanghaifeng.mis.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String login() {
return SUCCESS;
}
}
访问结果如下: