zoukankan      html  css  js  c++  java
  • Struts2学习笔记(二):第一个Struts2应用

    一、创建Action类。

      创建工程Struts2Demo

      struts 2中的Action类并不需要继承struts 2中的某个父类,普遍的java类就可以。

      在org.sunny.user.action包中创建名为UserAction的类。

      

    package org.sunny.user.action;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionContext;
    
    public class UserAction {
        //与JSP文件中属性名称相对应
        private String username ;
        private String message ;
        //创建属性的setter和getter方法
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
    public String execute(){ return "success" ; } public String sendFeedback(){ StringBuffer msg = new StringBuffer("你好,") ; msg.append(this.username).append(",消息:'").append(message).append("'已经发送完毕!"); ServletActionContext.getRequest().setAttribute("feedback", msg.toString()); return "success" ; } }

    二、配置Action类。

      在struts.xml中加入如下配置。

    <package name="sunny" namespace="/" extends="struts-default">
            <action name="user_*" class="org.sunny.user.action.UserAction" method="{1}">
                <result name="success">${pageContext.request.contextPath}/WEB-INF/jsp/success.jsp</result>
            </action>
    </package>

      class="org.sunny.user.action.UserAction"对应的就是我们所创建的类。

      action中的name属性就是我们访问网页时的路径名称,如果设置为'name="user“',则访问时就为"localhost:8080/Struts2Demo/user.action"。

      action中的methos属性设置是访问这个action类时所访问的方法名称,默认是"execute",即,访问action类中的execute()方法。

      methos属性这里设置成'{1}'是与前面的name属性的设置相对应,name设置为'user_*',就可以在访问时自己选择访问action的那个方法,如果访问路径为"localhost:8080/Struts2Demo/user_sendFeedback.action",这表明将访问类中的sendFeedback()方法,等同于'method="sendFeedback"'。

      action中的result属性设置的为页面跳转的路径,result中的name属性设置的是action类中访问的方法返回的字符串,后面的值${pageContext.request.contextPath}/WEB-INF/jsp/success.jsp就是页面跳转的路径。如,result中name属性设置为'success',如果execute()方法最后return "success",则浏览器将会跳转到WEB-INF/jsp/success.jsp,${pageContext.request.contextPath}表示的是项目的根路径。一个action中可以设置多个result,但是各个result的name属性的值应当不一样。

    三、创建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>
    </head>
    <body>
        <h1>struts2 demo 首页</h1>
        <form action="${pageContext.request.contextPath }/user_sendFeedback.action">
            姓名:<input type="text" name="username"><br>
            消息:<input type="text" name="message" ><br>
            <input type="submit" value="发送">
        </form>
    </body>
    </html>

      注:两个文本框中的name属性必须和UserAction中的属性名称一致,否则将不能讲值传入UserAction中的两个属性中,这种传值得原理运用到了java中的反射机制。

      WEB-INF/jsp中创建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>
    </head>
    <body>
        <h1>you had send message success!</h1>
        <%=request.getAttribute("feedback")%>
    </body>
    </html>

    四、运行效果。

  • 相关阅读:
    Educational Codeforces Round 72 (Rated for Div. 2)
    2249: Altruistic Amphibians 01背包
    lh的简单图论
    E. XOR Guessing 交互题 Educational Codeforces Round 71 (Rated for Div. 2)
    C. Helga Hufflepuff's Cup 树形dp 难
    B. Marvolo Gaunt's Ring 前缀后缀
    android学习-IPC机制之ACtivity绑定Service通信
    大数组分时加载算法 timedChunk
    log4j 配置和使用
    fastjson 配置和使用
  • 原文地址:https://www.cnblogs.com/FlyingPuPu/p/5217591.html
Copyright © 2011-2022 走看看