zoukankan      html  css  js  c++  java
  • Struts2入门示例

    1、导入struts2需要的9个包到lib文件夹中

    2、配置web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      
      <filter>
          <filter-name>struts</filter-name>
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      
      <filter-mapping>
          <filter-name>struts</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    如上面所示:配置了StrutsPrepareAndExecuteFilter,StrutsPrepareAndExecuteFilter就会自动加载struts2框架。配置好了filter后,还需要配置filter拦截的URL。一般让拦截用户所有请求。配置filter还可以是org.apache.struts2.dispatcher.FilterDispatcher,但是该filter已近过时了,所有还是用StrutsPrepareAndExecuteFilter

    3、Action控制器

    在src/action文件夹下面创建一个ShowWords类

     1 package action;
     2 
     3 public class ShowWords {
     4 
     5     private String name;
     6     private String words;
     7     public String getName() {
     8         return name;
     9     }
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     public String getWords() {
    14         return words;
    15     }
    16     public void setWords(String words) {
    17         this.words = words;
    18     }
    19     
    20     public String execute(){
    21         if(name.equals("")){
    22             return "input";
    23         }else{
    24             words="欢迎您:"+name;
    25             return "success";
    26         }
    27     }
    28 }

    如上ShowWords类,添加了name和words属性,并在action中执行默认方法execute,判断如果字符串为空,则返回逻辑视图input,否则返回success逻辑视图

    4、配置struts.xml

    在src下面新建一个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>
        <constant name="struts.action.extension" value="action,do,"></constant>
        <package extends="struts-default" name="default" namespace="/">
            <action name="showWords" class="action.ShowWords">
                <result>/Hello.jsp</result>
                <result name="input">/index.jsp</result>
            </action>
        </package>
    </struts>

    其中dtd名称空间可以拷贝struts2-core-2.3.28.1.jar包下面struts-default.xml下面的DOCTYPE

    constant:用于配置struts默认配置,例如struts.i18n.encoding,用于设置字符编码;struts.action.extension:用于设置url的扩展名,这里设置可以不需要扩展名,也可以用action或do作为扩展名

    struts框架默认属性保存在struts2-core-2.3.28.1.jar/org.apache.struts2/default.properties中

    extends="struts-default":表示该配置继承自srtuts-default,而struts-default配置在struts-default.xml文件中

    <action name="showWords" class="action.ShowWords">

    name:url访问路径   class:action类

    在action标签中还有一个method属性,该属性用来指定需要执行action类中的什么方法,如果不指定,默认方法execute方法

    <result>/Hello.jsp</result>
    <result name="input">/index.jsp</result>

    根据action类返回结果,来控制视图

    name属性即为action的返回值,如果不设置name默认为success,而result中文本表示需要执行的视图jsp的地址

  • 相关阅读:
    Visual Studio 2008 完全卸载
    设置 Visual Studio 文件版权信息
    安装 Visual Studio 插件 Visual Assist
    下载 / 安装 Visual Studio
    Python help 函数
    Python next 函数
    Python oct 函数
    Python min 函数
    Python reload 函数
    numpy中matrix的特殊属性
  • 原文地址:https://www.cnblogs.com/caoyc/p/5579747.html
Copyright © 2011-2022 走看看