zoukankan      html  css  js  c++  java
  • 【SSH框架】之Struts2系列(二)

    微信公众号:compassblog

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

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

    1、Struts2常量配置

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

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

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

    <constant name="struts.i18n.encoding" value="UTF-8"></constant>

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

    文件具体路径如下图:

    文件配置代码为:

    struts.i18n.encoding=UTF8

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

      <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:最后加载核心过滤器及自己的常量的配置

    2、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{
       //具体方法
    }

    3、通过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.7、Tomcat7.0

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

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

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

    本号文章可以任意转载,转载请注明出处!

    您可能还喜欢:

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

  • 相关阅读:
    【转】字典转模型需要注意的问题,以及第三方框架来处理字典转模型
    【转】使用SOAP访问Web服务
    Foundation框架2
    Foundation框架1
    什么是Protocol
    什么是Block
    什么么是Category
    ARC
    autorelease简介
    循环retain
  • 原文地址:https://www.cnblogs.com/compassblog/p/8454543.html
Copyright © 2011-2022 走看看