【SSH框架】之Struts2系列(二)

微信公众号:compassblog
html

欢迎关注、转发,互相学习,共同进步!前端

有任何问题,请后台留言联java

一、Struts2常量配置

(1)、Struts2默认常量配置文件路径,以下图:web

(2)、Struts2常量配置方式:以配置国际化字节编码UTF-8为例apache

方式1:在struts.xml文件中配置浏览器

<constant name="struts.i18n.encoding" value="UTF-8"></constant>
复制代码

方式2:在src下建立struts.properties文件配置bash

文件具体路径以下图:微信

文件配置代码为:框架

struts.i18n.encoding=UTF8
复制代码

方式3:在项目的web.xml文件中配置dom

<context-param>
      <param-name>struts.i18n.encoding</param-name>
      <param-value>UTF-8</param-value>
  </context-param>
复制代码

(3)、Struts2配置文件加载顺序:

  • defalut.properties:首先加载默认配置

  • struts.xml:其次加载Action及本身的常量配置

  • struts.properties:第三加载文件中的常量配置

  • web.xml:最后加载核心过滤器及本身的常量的配置

二、Struts2中Action类的书写方式:

方式1:直接建立一个类

package com.struts2.action;

//建立一个POJO类:不继承任何父类,不实现任何接口
public class Demo1 {
    //具体的方法
}
复制代码

方式2:建立一个类实现Action接口

package com.struts2.action;

import com.opensymphony.xwork2.Action;

//实现一个Action接口
// 覆写excute方法,提供action方法的规范
// Action接口预置了一些字符串,以在返回结果时使用
public class Demo2 implements Action {

    @Override
    public String execute() throws Exception {
        //具体代码实现
        return null;
    }

}
复制代码

方式3:建立一个类继承ActionSupport类

package com.struts2.action;

import com.opensymphony.xwork2.ActionSupport;

//该类已经实现了Validateable, ValidationAware, TextProvider, LocaleProvider接口
//当项目须要使用这些接口时,不须要本身来实现
public class Demo3 extends ActionSupport{
    //具体方法
}
复制代码

三、经过form表单测试Struts2应用小实例

(1)、新建项目ExamplePeoject,导入所须要的jar包,搭建Struts2开发环境,以下图:

(2)、在WebContent目录下分别建一个index.jsp访问文件、success.jsp跳转文件、failure.jsp跳转文件,具体代码以下:

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>首页</title>
</head>
<body>
    <h1>Struts2项目测试实例</h1>
    <form action="${pageContext.request.contextPath}/example" method="post">
        <table>
            <tr>    
                <td>输入测试内容:<input type="text" name="content" id="content"></td>
                <td><input type="submit" value="点击测试"></td>
            </tr>
        </table>
    </form>
</body>
</html>
复制代码

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>跳转页</title>
</head>
<body>
    <h1>页面成功跳转,Struts2实例项目测试成功!</h1>
</body>
</html>
复制代码

failure.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>跳转页</title>
</head>
<body>
    <h1>页面跳转成功,但输入内容不正确!</h1>
</body>
</html>
复制代码

(3)、配置struts.xml文件,具体代码以下:

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.i18n.encoding" value="UTF-8"></constant>

    <package name="test" namespace="/" extends="struts-default" >

        <action name="example" class="com.struts2.action.ExampleAction" method="test" >
            <result name="success">/success.jsp</result>
            <result name="failure">/failure.jsp</result>
        </action>
    </package>
</struts>
复制代码

(4)、新建Action类ExampleAction.java,具体代码以下:

ExampleAction.java

package com.struts2.action;

import com.struts2.domain.Content;

public class ExampleAction {
    //定义内容属性
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String test(){

        //获取客户端内容,并进行内容判断
        if(getContent()!=null){
            if(getContent().equals("Struts2"))
                return "success";
        }
        return "failure";
    }
}
复制代码

(5)、发布项目到Tomcat容器,而后到浏览器地址栏测试,结果以下:

跳转到首页:

输入Struts2进行测试:

输入Struts进行测试:

(6)、该实例项目访问流程,如图所示:


本项目运行环境:jdk1.七、Tomcat7.0


关注微信公众号compassblog,后台回复 “Struts2系列二” 获取本项目源码


本系列后期仍会持续更新,欢迎关注!

若是你认为这篇文章有用,欢迎转发分享给你的好友!

本号文章能够任意转载,转载请注明出处!


您可能还喜欢:


                          扫码关注微信公众号,了解更多

                               

相关文章
相关标签/搜索