美文网首页struts2
struts2 ognl表达式#和%的使用

struts2 ognl表达式#和%的使用

作者: DouDouZH | 来源:发表于2018-05-18 20:49 被阅读0次

    一、#的使用

    1、获取contex里面的数据
    2、演示#操作
    (1)步骤
    • 向request放值
    • 用ognl获取
    • 获取context 里的数据,写ognl的时候添加#context的key名称.域对象名称
    (2)代码

    ValuesStackAction5.java

    package work.zhangdoudou.Action;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    import work.zhangdoudou.Bean.User;
    
    public class ValuesStackAction5 extends ActionSupport{
        
        @Override
        public String execute() throws Exception {
            HttpServletRequest request=ServletActionContext.getRequest();
            request.setAttribute("name", "zhangsan");
            
            return SUCCESS;
        }
    }   
    

    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.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
        <package name="default" namespace="/" extends="struts-default">
            <action name="userAction5" class="work.zhangdoudou.Action.ValuesStackAction5" method="execute">
                <result name="success">/ValuesStack.jsp</result>
            </action>   
        </package>
    </struts>
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>struts2_ognl</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <filter>
        <filter-name>struts2</filter-name>
        <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>
    

    ValuesStack.jsp页面

     <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>  
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'ValuesStackAction.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      <body>
       <s:debug></s:debug>
          <!-- 获取context 里的数据,写ognl的时候添加#context的key名称.域对象名称 -->
          <s:property value="#request.name"/>
      </body>
    </html>
    
    (3)运行效果
    image.png

    二、%的使用

    1、在struts2标签中表单标签
    (1)在struts2标签里面使用ognl表达式,如果直接在struts表单里使用ognl表达式不能识别,只有加%才能识别
    (2)代码

    ValuesStackAction5.java

    package work.zhangdoudou.Action;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    import work.zhangdoudou.Bean.User;
    
    public class ValuesStackAction5 extends ActionSupport{
        
        @Override
        public String execute() throws Exception {
            HttpServletRequest request=ServletActionContext.getRequest();
            request.setAttribute("name", "zhangsan");
            
            return SUCCESS;
        }
    }   
    

    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.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
        <package name="default" namespace="/" extends="struts-default">
            <action name="userAction5" class="work.zhangdoudou.Action.ValuesStackAction5" method="execute">
                <result name="success">/ValuesStack.jsp</result>
            </action>   
        </package>
    </struts>
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>struts2_ognl</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <filter>
        <filter-name>struts2</filter-name>
        <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>
    

    ValuesStack.jsp页面

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>  
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>My JSP 'ValuesStackAction.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      <body>
       <s:debug></s:debug>
          <hr>
          ognl%的使用<br>
          username<s:textfield name="username" value="%{#request.name}"></s:textfield>
      </body>
    </html>
    
    (3)效果
    image.png

    相关文章

      网友评论

        本文标题:struts2 ognl表达式#和%的使用

        本文链接:https://www.haomeiwen.com/subject/vjdndftx.html