Struts2入门

Struts的官方网站http://struts.apache.org,在该网站上可以获取Struts的所有版本及帮助文档。Struts2框架主要是通过一个过滤器将Struts集成到Web应用。

1. 第一个Struts2程序

例1.1 创建Java Web项目,添加Struts2的支持类库,通过Struts2将请求转发到指定的JSP页面。

(1)创建名称为java_struts2的Web项目,将Struts2的支持类库文件添加到WEB-INF目录的lib文件夹中(注:也可以让MyEclipse自动添加该jar包,避免不兼容的错误),由于本实例实现功能比较简单,所以只添加Struts2的核心类包即可。添加后效果如下所示:

(2)在web.xml文件中声明Struts2提供的过滤器,类名为org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。关键代码如下:

<?xml version="1.0" encoding="GBK"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>java_struts2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- Struts2过滤器 -->
  <filter>
      <!-- 过滤器名称 -->
      <filter-name>struts2</filter-name>
      <!-- 过滤器类 -->
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <!-- Struts2过滤器映射 -->
  <filter-mapping>
      <!-- 过滤器名称 -->
      <filter-name>struts2</filter-name>
      <!-- 过滤器映射 -->
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

(3)在Web项目的源码文件夹中,创建名称为struts.xml的配置文件,在此配置文件中定义Struts2中的Action对象。关键代码如下:

<?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>
    <!-- 声明包 -->
    <package name="myPackage" extends="struts-default">
        <!-- 定义action -->
        <action name="first">
            <!-- 定义处理成功后的映射页面 -->
            <result>/first.jsp</result>
        </action>
    </package>
</struts>

(4)创建程序中的主页面index.jsp,在该页面中编写一个超链接,用于访问上面所定义的Action对象,此链接所指向的地址为first.action。关键代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <a href="first.action">请求Struts2</a>
  </body>
</html>

(5)创建名称为first.jsp的JSP页面,此页面是Action对象first处理成功后的返回页面。关键代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    第一个Struts2程序!
    <br>
</body>
</html>

 2. 动态Action的应用

例2.1 创建一个Java Web项目,应用Struts2提供的动态Action,处理添加用户信息请求及更新用户信息的请求。

(1)创建Java Web项目,将Struts2的支持类型库文件添加到WEB-INF目录的lib文件夹中。之后在web.xml文件中注册Struts2提供的过滤器。

(2)创建名称为UserAction的Action对象,并在这个Action对象中分别编写add()方法与update()方法,用于处理添加用户信息的请求及更新用户信息的请求,并将请求返回到相应的页面。关键代码如下:

package com.cn.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    //提示信息
    private String info;
    //添加用户信息
    public String add() throws Exception{
        info = "添加用户信息";
        return "add";
    }
    //更新用户信息
    public String update() throws Exception{
        info = "更新用户信息";
        return "update";
    }
    
    public String getInfo() {
        return info;
    }
    
    public void setInfo(String info){
        this.info = info;
    }
}

实例中主要演示Struts2的动态Action的处理方式,并没有进行实际用户的添加与更新操作,add()方法与update()方法对请求处理的方式非常简单,只对UserAction类中的info变量赋了一个值,并返回了相应的结果。

(3)在Web项目的源码文件夹(MyEclipse中默认为src目录)中,创建名称为struts.xml的配置文件,在此配置文件中配置UserAction。关键代码如下:

<?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>
    <!-- 声明包 -->
    <package name="myPackage" extends="struts-default">        
        <!-- 定义action -->
        <action name="userAction" class="com.cn.action.UserAction">
            <!-- 添加成功的映射页面 -->
            <result name="add">user_add.jsp</result>
            <!-- 更新成功的映射页面 -->
            <result name="update">user_update.jsp</result>
        </action>
    </package>
</struts>

(4)创建名称为user_add.jsp的JSP页面,它是成功添加用户信息的返回页面。关键代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ 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=GB18030">
<title>Insert title here</title>
</head>
<body>
    <font color="red">
        <s:property value="info"/>
    </font>
</body>
</html>

在user_add.jsp页面中,实例中通过Struts2标签输出UserAction中的信息,也就是UserAction中的add()方法为info属性所赋的值。

(5)创建名称为user_update.jsp的JSP页面,它是成功更新用户信息的返回页面。关键代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ 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=GB18030">
<title>Insert title here</title>
</head>
<body>
    <font color="red">
        <s:property value="info"/>
    </font>
</body>
</html>

(6)创建程序中的首页index.jsp,在该页面中添加两个超链接,通过Struts2提供的动态Action功能,将这两个超链接请求分别指向用于UserAction类的添加用户信息的请求与更新用户的请求。关键代码如下:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    <a href="userAction!add">添加用户</a>
    <br>
    <a href="userAction!update">更新用户</a>
</body>
</html>

注意:使用Struts2的动态Action,其Action请求的URL地址中使用“!"号分隔Action请求与请求字符串,而请求字符串的名称需要与Action类中的方法名称相对应,否则将出现java.lang.NoSuchMethodException异常。

原文地址:https://www.cnblogs.com/gaopeng527/p/4465245.html