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>

四、运行效果。

 

转载于:https://www.cnblogs.com/FlyingPuPu/p/5217591.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值