Structs复习 Structs标签

如果类型是object Structs会把它默认解析为OGNL表达式

想取字符串的话就 ‘’ ‘ ’ ‘’

jar包

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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
        <filter-name>struts2</filter-name>
        <!--<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

Structs.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <!--<constant name="struts.enable.DynamicMethodInvocation" value="false" />-->
    <constant name="struts.devMode" value="true" />
    <!--<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>-->
    <constant name="struts.ui.theme" value="simple" />
    <package name="tags" extends="struts-default">

        <action name="tags" class="com.bjsxt.struts2.tags.TagsAction">
            <result>/tags.jsp</result>
        </action>

    </package>

</struts>

Dog.java

package com.bjsxt.struts2.tags;

public class Dog {
    
    private String name;
    
    public Dog() {
        
    }

    public Dog(String name) {
        super();
        this.name = name;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "dog: " + name;
    }
}

S.hava

package com.bjsxt.struts2.tags;

public class S {
    public static String STR = "STATIC STRING";
    
    public static String s() {
        return "static method";
    }
}

TagsAction.java

package com.bjsxt.struts2.tags;

import com.opensymphony.xwork2.ActionSupport;

public class TagsAction extends ActionSupport {

    private String password;

    private String username;

    public TagsAction() {
    }

    public String execute() {
        this.addFieldError("fielderror.test", "wrong!");
        return SUCCESS;
    }
    
    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
}

User.java

package com.bjsxt.struts2.tags;

public class User {
    private int age = 8;
    
    public User() {
        
    }
    
    public User(int age) {
        super();
        this.age = age;
    }


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "user" + age;
    }
}

index.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
    访问属性
    <a href="http://localhost:6666//Struts2_2000_StrutsTags/tags.action?username=u&password=p">tags</a>
</body>
</html>

具体用法建议使用的时候再回来参考

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Struts-Tags学习</title>
</head>
<body>
    <ol>
        <li>property: <s:property value="username"/> </li>
        <li>property 取值为字符串: <s:property value="'username'"/> </li>
        <li>property 设定默认值: <s:property value="admin" default="管理员"/> </li>
        <li>property 设定HTML: <s:property value="'<hr/>'" escape="false"/> </li>
        <hr />
        <li>set 设定adminName值(默认为request 和 ActionContext): <s:set var="adminName" value="username" /></li>
        
        <li>set 从request取值: <s:property value="#request.adminName" /></li>
        <li>set 从ActionContext取值: <s:property value="#adminName" /></li>
        
        <%--<li>set 设定范围: <s:set name="adminPassword" value="password" scope="page"/></li>
        <li>set 从相应范围取值: <%=pageContext.getAttribute("adminPassword") %></li>
        --%>
        <li>set 设定var,范围为ActionContext: <s:set var="adminPassword" value="password" scope="session"/></li>
        <li>set 使用#取值: <s:property value="#adminPassword"/> </li>
        <li>set 从相应范围取值: <s:property value="#session.adminPassword"/> </li>
        
        <hr />
        
        <%--<li>push:<s:set name="myDog" value="new com.bjsxt.struts2.ognl.Dog('oudy')"></s:set></li>
        <li>
        push:<s:push value="#myDog">
            <s:property value="name"/>
        </s:push>
        </li>
        <li>push: <s:property value="name"/></li>
        --%>
        
        <hr />
        <li>bean 定义bean,并使用param来设定新的属性值:
            <s:bean name="com.bjsxt.struts2.tags.Dog" >
                <s:param name="name" value="'pp'"></s:param>
                <s:property value="name"/>
                
            </s:bean>
            
            
        </li>
        
        <li>bean 查看debug情况:
            <s:bean name="com.bjsxt.struts2.tags.Dog" var="myDog">
                <s:param name="name" value="'oudy'"></s:param>
            </s:bean>
            拿出值:
            <s:property value="#myDog.name"/>
            
        </li>
        <hr />
        
        <li>include _include1.html 包含静态英文文件
        <s:include value="/_include1.html"></s:include>
        </li>
        
        <li>include _include2.html 包含静态中文文件
        <s:include value="/_include2.html"></s:include>
        </li>
        
        <li>include _include1.html 包含静态英文文件,说明%用法
        <s:set var="incPage" value="%{'/_include1.html'}" />
        <s:include value="%{#incPage}"></s:include>
        </li>
        
        
        <hr />
        
        <li>if elseif else: 
        age = <s:property value="#parameters.age[0]" /> <br />
        <s:set var="age" value="#parameters.age[0]" />
        <s:if test="#age < 0">wrong age!</s:if>
        <s:elseif test="#parameters.age[0] < 20">too young!</s:elseif>
        <s:else>yeah!</s:else><br />
        
        <s:if test="#parameters.aaa == null">null</s:if>
        </li>
        
        <hr />
        
        <li>遍历集合:<br />
        <s:iterator value="{1, 2, 3}" >
            <s:property/> |
        </s:iterator>
        </li>
        <li>自定义变量:<br />
        <s:iterator value="{'aaa', 'bbb', 'ccc'}" var="x">
            <s:property value="#x.toUpperCase()"/> |
        </s:iterator>
        </li>
        <li>使用status:<br />
        <s:iterator value="{'aaa', 'bbb', 'ccc'}" status="status">
            <s:property/> | 
            遍历过的元素总数:<s:property value="#status.count"/> |
            遍历过的元素索引:<s:property value="#status.index"/> |
            当前是偶数?:<s:property value="#status.even"/> |
            当前是奇数?:<s:property value="#status.odd"/> |
            是第一个元素吗?:<s:property value="#status.first"/> |
            是最后一个元素吗?:<s:property value="#status.last"/>
            <br />
        </s:iterator>
        
        </li>
        
        <li>
        <s:iterator value="#{1:'a', 2:'b', 3:'c'}" >
            <s:property value="key"/> | <s:property value="value"/> <br />
        </s:iterator>
        </li>
        
        <li>
        <s:iterator value="#{1:'a', 2:'b', 3:'c'}" var="x">
            <s:property value="#x.key"/> | <s:property value="#x.value"/> <br />
        </s:iterator>
        </li>
        
        <li>
        
        <s:fielderror fieldName="fielderror.test" theme="simple"></s:fielderror>
        
        </li>
    </ol>
    
    
    
    
</body>
</html>

include.jsp

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>_include</title>
</head>
<body>
params :<br />
使用property取参数:p1=<s:property value="#parameters.p1"/> <br />
使用property取参数:p2=<s:property value="#parameters.p2"/> <br />
使用property取参数:p3=<s:property value="#parameters.p3"/> <br />
</body>
</html>


 

_include1.html

 

posted @ 2018-08-13 17:08  橘柑之味  阅读(213)  评论(0编辑  收藏  举报