• Struts2:


    pom.xml:

    <packaging>war</packaging>
    <dependencies>

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <port>9001</port>
                </configuration>
            </plugin>
        </plugins>
    </build>

    web.xml:

    <!--配置文件的加载顺序
        struts.xml
        struts.properties
        web.xml
    -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <filter>
        <!--指定struts2的核心过滤器-->
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <!--通过init-param元素配置struts常量-->
        <init-param>
            <param-name>struts.i18n.encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    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 name="struts.devMode" value="true"/>
        <!--请求的扩展名为action或空-->
        <constant name="struts.action.extension" value="action,"/>
        <!--开启动态方法访问-->
        <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
        <package name="demo1" extends="struts-default" namespace="/">
            <!--全局结果页面-->
            <global-results>
                <result name="success">/success.jsp</result>
            </global-results>
            <!--method默认为execute-->
            <action name="strutsDemo1" class="com.fly.action.action01" method="execute">

            </action>
            <!--方法一-->
            <!--<action name="user_*" class="com.fly.action.userAction" method="{1}">-->
            <!--方法二 使用动态-->
            <action name="user" class="com.fly.action.userAction">

            </action>
            <action name="requestDemo" class="com.fly.action.requestDemo">

            </action>
            <action name="requestDemo2" class="com.fly.action.requestDemo2">
                <result name="success">/success2.jsp</result>
            </action>
        </package>
    </struts>

    index.jsp:

    <body>
    <a href="${pageContext.request.contextPath}/strutsDemo1.action">/strutsDemo1.action</a><br>
    <h4>User管理:</h4>
    <a href="${pageContext.request.contextPath}/user!find">查询用户</a><br>
    <a href="${pageContext.request.contextPath}/user!update">修改用户</a><br>
    <a href="${pageContext.request.contextPath}/user!delete">删除用户</a><br>
    <hr>
    <h4>servlet的api</h4>
        <form action="${pageContext.request.contextPath}/requestDemo2" method="post">
            name:<input type="text" name="name">
            age:<input type="text" name="age">
            <input type="submit">
        </form>

    </body>

    success.jsp:

    <body>
    <h4>成功</h4>
    ${requestName_name}
    ${requestName_age}
    ${sessionName_name}
    ${sessionName_age}
    ${applicationName_name}
    ${applicationName_age}
    </body>

    success2.jsp:

    <body>
    <h4>成功</h4>
    ${requestScope.name1}
    ${sessionScope.name2}
    ${applicationScope.name3}
    </body>

    action01:

    public class action01 {
        public String execute(){
            System.out.println("执行了");
            /**
             * NONE 代表页面不跳转
             */
            return "success";
        }
    }

    userAction:

    public class userAction extends ActionSupport {
        public String find(){
            System.out.println("查询联系人");
            return NONE;
        }
        public String update(){
            System.out.println("修改联系人");
            return NONE;
        }
        public String delete(){
            System.out.println("删除联系人");
            return NONE;
        }
    }

    requestDemo:

    /**
     * 访问servlet api方式一:不直接获取实例
     */
    public class requestDemo extends ActionSupport {
        @Override
        public String execute() throws Exception {
            //接受表单的参数
            //使用struts2的ActionContext对象
            ActionContext context = ActionContext.getContext();
            Map<String, Object> parameters = context.getParameters();
            for (String s : parameters.keySet()) {
                String[] v = (String[]) parameters.get(s);
                System.out.println(s+":"+v[0]);
                //向request中存数据
                context.put("requestName_"+s,v[0]);
                //向session中存数据
                context.getSession().put("sessionName_"+s,v[0]);
                //向application中存数据
                context.getApplication().put("applicationName_"+s,v[0]);
            }
            return SUCCESS;
        }
    }

    requestDemo2:

    public class requestDemo2 extends ActionSupport implements ServletRequestAware {
        HttpServletRequest request;
        @Override
        public String execute() throws Exception {
            request = ServletActionContext.getRequest();
            //接受表单的参数
            String name = this.request.getParameter("name");
            String age = this.request.getParameter("age");
            System.out.println(name+":"+age);
            //向request中存数据
            this.request.setAttribute("name1",name+"1");
            this.request.getSession().setAttribute("name2",name+"2");
            this.request.getSession().getServletContext().setAttribute("name3",name+"3");
            return SUCCESS;
        }

        public void setServletRequest(javax.servlet.http.HttpServletRequest request) {
    //        this.request = request;
        }
    }

    数据封装:

    属性驱动:

    /**
     * 属性封装驱动方式一:属性set
     */
    public class dataDemo1 extends ActionSupport {
        //接受参数
        private String name;
        private int age;

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

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String execute() throws Exception {
            System.out.println(name+":"+age);
            return NONE;
        }
    }

    dataDemo2:

    //页面提供表达式

    public class dataDemo2 extends ActionSupport {
        //接受参数
        private User user;
        //还需提供对象的get
        public User getUser() {
            return user;
        }

        public void setUser(User user) {
            this.user = user;
        }

        @Override
        public String execute() throws Exception {
            System.out.println(user);
            return NONE;
        }
    }

    dataDemo3:

    public class dataDemo3 extends ActionSupport implements ModelDriven<User> {
        private User user = new User();

        public User getModel() {
            return user;
        }
        @Override
        public String execute() throws Exception {
            System.out.println(user);
            return NONE;
        }

    }

    dataDemo4:

    /**
     * 封装list
     */
    public class dataDemo4 extends ActionSupport{
        private List<User> list;

        public void setList(List<User> list) {
            this.list = list;
        }

        public List<User> getList() {
            return list;
        }

        @Override
        public String execute() throws Exception {
            for (User user : list) {
                System.out.println(user);
            }
            return NONE;
        }

    }

    dataDemo5:

    /**
     * 封装map
     */
    public class dataDemo5 extends ActionSupport{
        private Map<String,User> map;

        public Map<String, User> getMap() {
            return map;
        }

        public void setMap(Map<String, User> map) {
            this.map = map;
        }

        @Override
        public String execute() throws Exception {
            for (String s : map.keySet()) {
                System.out.println(map.get(s));
            }
            return NONE;
        }

    }

    dataDriver.jsp:

    <body>
    <h1>属性驱动</h1>
    <h2>方式一:提供set方法</h2>
    <form action="${pageContext.request.contextPath}/datademo1" method="post">
        name:<input type="text" name="name">
        age:<input type="text" name="age">
        <input type="submit">
    </form>
    <h2>方式二:页面提供表达式</h2>
    <form action="${pageContext.request.contextPath}/datademo2" method="post">
        name:<input type="text" name="user.name">
        age:<input type="text" name="user.age">
        <input type="submit">
    </form>
    <hr>
    <h1>模型驱动</h1>
    <form action="${pageContext.request.contextPath}/datademo3" method="post">
        name:<input type="text" name="name">
        age:<input type="text" name="age">
        <input type="submit">
    </form>
    <h1>封装list</h1>
    <form action="${pageContext.request.contextPath}/datademo4" method="post">
        name:<input type="text" name="list[0].name">
        age:<input type="text" name="list[0].age">
        name:<input type="text" name="list[1].name">
        age:<input type="text" name="list[1].age">
        <input type="submit">
    </form>
    <h1>封装set</h1>
    <form action="${pageContext.request.contextPath}/datademo5" method="post">
        name:<input type="text" name="map['A'].name">
        age:<input type="text" name="map['A'].age">
        name:<input type="text" name="map['B'].name">
        age:<input type="text" name="map['B'].age">
        <input type="submit">
    </form>
    </body>

     

    EL特殊字体的使用:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
        <title>EL的特殊字符的使用</title>
    </head>
    <body>
    <h1>EL的特殊字符的使用</h1>
    <h2>#号的使用</h2>
    <s:property value="#request.name"/>
    <br>
    <s:iterator value="#{'aaa':'111','bbb':'222'}" var="entry">
        <s:property value="key"/>----<s:property value="value"/>
        <s:property value="#entry.key"/>----<s:property value="#entry.value"/>
    </s:iterator>
    <br><hr>
    <s:iterator value="list" var="entry1" status="st">
        <ul>
            <li><s:property value="list[#st.index].name"/>----<s:property value="list[#st.index].age"/></li>
            <li><s:property value="#entry1.name"/>----<s:property value="#entry1.age"/></li>
        </ul>
    </s:iterator>
    <br>
    <s:radio list="#{'1':'男','2':'女'}" name="sex"/>
    <h2>%号的使用</h2>
    <%--强制解析OGNL表达式--%>
    <s:textfield name="name" value="%{#request.name}"/>
    <br>
    <s:property value="%{#request.name}"/>
    <br>
    <%--强制不解析OGNL表达式--%>
    <s:property value="%{'#request.name'}"/>
    </body>
    </html>

     

    拦截器:

    PriInterceptor

    public class PriInterceptor extends MethodFilterInterceptor {
        protected String doIntercept(ActionInvocation invocation) throws Exception {
            System.out.println("拦截器执行了");
            User1 existUser = (User1) ServletActionContext.getRequest().getSession().getAttribute("existUser");
            if (existUser==null){
                ActionSupport actionSupport = (ActionSupport) invocation.getAction();
                actionSupport.addActionError("没有登录");
                return actionSupport.LOGIN;
            }else {
                return invocation.invoke();
            }
        }
    }

    <!--定义拦截器-->
    <interceptors>
        <interceptor name="priInterceptor" class="com.fly.interceptor.PriInterceptor"/>
        <interceptor-stack name="myStack">
            <interceptor-ref name="priInterceptor"/>
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>
    </interceptors>

    action中配置:

    <action name="user" class="com.fly.action.userAction">
        <interceptor-ref name="myStack"/>
    </action>

    标签使用:

    <!--指定模板-->
    <constant name="struts.ui.theme" value="simple"/>

    Jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <s:if test="1>2">
        1>2
    </s:if>
    <s:elseif test="2>1">
        2>1
    </s:elseif>
    <s:else>
        2=1
    </s:else>
    <hr>
    <table border="1">
        <tr>
            <th>索引</th>
            <th>个数</th>
            <th></th>
            <th>第一个</th>
            <th>最后一个</th>
            <th>偶数</th>
            <th>奇数</th>
        </tr>
        <%--begin默认是0--%>
        <s:iterator var="v" begin="1" end="10" step="2" status="st">
            <tr>
                <td><s:property value="#st.index"/></td>
                <td><s:property value="#st.count"/></td>
                <td><s:property value="#v"/></td>
                <td><s:property value="#st.first"/></td>
                <td><s:property value="#st.last"/></td>
                <td><s:property value="#st.even"/></td>
                <td><s:property value="#st.odd"/></td>
            </tr>
        </s:iterator>
    </table>
    <s:property value="'www.baidu.com'"/><br>
    <s:property value="" default="123"/><br>
    <%--忽略html代码--%>
    <s:property value="'<h3>www.baidu.com<h3>'" escape="true"/><br>
    <%--不忽略--%>
    <s:property value="'<h3>www.baidu.com<h3>'" escape="false"/><br>
    <s:a action="" namespace="/" href="index.jsp">index</s:a>
    <hr>
    <s:form action="" method="">
        <s:hidden name="" value=""/>
        <s:textfield label="用户名" name=""/>
        <s:password label="密码" name=""/>
        <s:radio label="性别" name="sex" list="#{'1':'男','2':'女'}" value="1"/>
        <s:checkboxlist label="爱好" name="" list="{'aa','bb','cc'}" labelposition="left"/>
        <s:select label="爱好" list="#{'1':'aaaa','2':'bbbb'}" listKey="key" listValue="value"/>
        <s:textarea label="描述" rows="5" cols="15"/>
        <s:file label="头像" name=""/>
        <s:reset value="重置"/>
        <s:submit value="提交"/>
    </s:form>
    </body>
    </html>

  • 相关阅读:
    Apache虚拟目录的建立
    自制户外登山地图傻瓜书
    经纬度与高克投影转换代码(VB)
    2000国家大地坐标系
    js格式化 Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间) 及相互转化
    Javascript农历与公历相互转换
    Numpy
    日期多选插件Kalendae.js
    Scrapy项目实战
    bootstrapdatetimepicker添加支持显示农历节假日信息。
  • 原文地址:https://www.cnblogs.com/fly-book/p/10145807.html
Copyright © 2020-2023  润新知