Struts2简单应用的实现

来源:互联网 发布:知乎和百度关系 编辑:程序博客网 时间:2024/06/06 05:42

一:struts2配置

访问struts2官网,下载完整包struts-2.5.10.1-all.zip(64M),解压缩之后lib目录下的便是我们需要引入的包。我们也可以下载struts-2.5.10.1-min-lib.zip,只有4M,里面是实现struts2最低要求的jar包,建议两个都下载。

  • commons-fileupload-1.3.2.jar
  • commons-io-2.4.jar
  • commons-lang3-3.4.jar
  • freemarker-2.3.23.jar
  • javassist-3.20.0-GA.jar
  • log4j-api-2.7.jar
  • ognl-3.1.12.jar
  • struts2-core-2.5.10.1.jar

下面开始配置

1.创建一个名为test的Dymamic Web Project,将jar包放进WebContent\WEB-INF\lib

2.进入struts-2.5.10.1-all\struts-2.5.10.1\apps目录解压缩struts2-showcase.war文件

3.进入struts2-showcase\WEB-INF目录找到web.xml文件,放进test\WebContent\WEB-INF目录

4.编辑web.xml文件,使其变得简洁(为什么需要示例包里的web.xml,因为我们需要的它的头部写法,版本变化后的写法有可能不同)

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"    id="WebApp_9" version="2.4">    <filter>        <filter-name>struts-prepare</filter-name>        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>    </filter>    <filter>        <filter-name>struts-execute</filter-name>        <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts-prepare</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>struts-execute</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></web-app>

5.进入struts2-showcase\WEB-INF\src\java目录找到struts.xml放到test\Java Resources\src下

6.编辑struts.xml(同样需要头部的写法),此时配置基本完成

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    "http://struts.apache.org/dtds/struts-2.5.dtd"><struts>    <!-- 这里加入<package>和<action> --></struts>

二:创建视图页面(1)

sayhello.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><html><head><title>Basic Struts 2 Application - Welcome</title></head><body>    <h3>你在街上看到了一个许久不见的朋友</h3>    <!--向action提交他的心情,相当于调用action中的setMood()方法-->    <form action="his.action" method="post">        <p>对方此时的心情</p>        <input type="radio" name="mood" value="good">好心情 <input            type="radio" name="mood" value="bad">坏心情 <br /> <input            type="submit" value="向他打个招呼">    </form></body></html>

三:创建Aciton动作类

hisAction.java

package com.action;import com.opensymphony.xwork2.ActionSupport;public class hisAction extends ActionSupport {    private String mood; // 动作属性    private String message;    //set、get方法    public String getMood() {return mood;}    public void setMood(String mood) {this.mood = mood;}    public String getMessage() {return message;}    public void setMessage(String message) {this.message = message;}    @Override    public String execute() throws Exception {        // 对方心情好时        if ("good".equals(mood)) {            setMessage("对方向你说道:I Love You!\n并吻了你一下");            return SUCCESS;        }        // 心情差        else if("bad".equals(mood)){            setMessage("对方冲你喊道:I Hate You!\n并揍了你一顿");            return ERROR;        }        //你没有打招呼,时光倒流,强制你再选择向心情好或坏的他打招呼        else            return INPUT;        }    }

Action接口中的常量解释

  1. SUCCESS:表示动作执行成功并把结果视图显示给用户
  2. ERROR:表示动作执行失败并把错误视图显示给用户
  3. INPUT:表示输入校验失败并把表单重新给用户填写
  4. LOGIN:表示登录失效并把登录视图显示给用户
  5. NONE:表示动作成功显示给用户任何视图

四:创建结果视图页面(2)

loveu.jsp
P.S.:该页面中使用了Struts2的标签,就要使用taglib指令<%@ taglib prefix=”s” uri=”/struts-tags”%>
导入标签库

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head><title>I Love U!</title></head><body>    <h2>        <!-- Struts框架调用动作类的getMessage(),获得对方的动作 -->        <s:property value="message" />    </h2></body></html>

hateu.jsp
P.S.:为了演示execute()返回常量,struts.xml根据常量返回视图,这里创建两个不同名但一样的视图;

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ include file="loveu.jsp" %>

五:修改struts.xml配置文件

<struts>    <!-- 这里加入<package>和<action> -->    <package name="whatever" extends="struts-default" namespace="/">        <action name="his" class="com.action.hisAction" method="execute">            <!-- 根据action的动作映射,如果SUCCESS,则返回loveu.jsp,如果ERROR,则返回hateu.jsp -->            <result name="success">/loveu.jsp</result>            <result name="error">/hateu.jsp</result>            <result name="input">/sayhello.jsp</result>        </action>    </package></struts>

六:运行程序

1:访问sayhello.jsp
向心情好或坏的他打个招呼吧

2:向好心情的他打个招呼
好心情的他

3:向坏心情的他打招呼
心情大大的坏的他

4:没有选择心情,跳转回sayhello.jsp,要求重新填写表单


七:执行过程

1.访问sayhello.jsp页面,用户选择向心情好坏,向his.action提交表单,相当于调用setMood()

2.容器接受对资源his.action的表单提交,根据web.xml配置文件,将所有的请求(/*)转发到org.apache.struts2.dispatcher.filter.StrutsPrepareFilterorg.apache.struts2.dispatcher.filter.StrutsExecuteFilter这两个 过滤类 ,该类对象是进入框架的入口;

3.Struts2框架在struts.xml文件中查找name=”his”的动作类,找到对应的类(com.action.hisAcion),Struts2实例化该类,并且调用execute()方法;

4.execute()返回相应的字符串常量(SUCCESS/ERROR/INPUT),struts.xml则根据常量返回相应的视图。

5.返回loveu.jsphateu.jsp视图时,标签会调用hisAction中的getMessage()方法,返回message的值


这里写图片描述

原创粉丝点击