SSH-struts

Struts框架的使用

1.编写Action类
2.Struts配置
1) 配置Struts核心过滤器
2) 配置Action映射
1. 导包
a) 注意:这个包是在Struts2与Spring整合的时候用的,单用Struts2的时候一定不能导这个包。
2. 编写Action类

import com.opensymphony.xwork2.ActionSupport;

public class DemoAction extends ActionSupport {

private static final long serialVersionUID = -466504590042207559L;

private String name;
private String password;

public DemoAction() {
   super();
}

public String getName() {
   return name;
}

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

public String getPassword() {
   return password;
}

public void setPassword(String password) {
   this.password = password;
}
@Override
public void validate() {
    System.out.println("validate:" + name + "\t" + password);

    if(name == null || "".equals(name)) {
        addFieldError("name", "用户名字段为空。");
    }
    if(password == null || "".equals(password)) {
        addFieldError("password", "密码字段为空。");
    }
}

 @Override
public String execute() {
    System.out.println("execute:" + name + "\t" + password);
    Title title = new Title();
    title.setTitle("起亚新K3开启汽车智能新时代");

    ActionContext ctx = ActionContext.getContext();
    HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
    //把title存储到请求作用域
    request.setAttribute("title", title);
    return SUCCESS;
}

}

  1. 在web.xml部署描述符中进行如下配置:

    struts2
    org.apache.struts2.dispatcher.FilterDispatcher


    struts2
    /*

  2. Action组件必须在struts.xml文件中进行配置后才能正常使用。Struts.xml文件必须位于应用程序的WEB-INF/classes中。

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

<struts>
    <package name="demo" namespace="/demo" extends="struts-default">
        <action name="DemoAction" class="com.justcrazy.www.action.DemoAction">
            <result name="success">/index.jsp</result>
            <result name="input">/error.jsp</result>
        </action>
    </package>
</struts>
  1. 编写Demo.jsp


This is Demo.



姓名:


密码:


注:以上是Struts2的应用

下一篇:SSH框架搭建过程—之Struts整合Spring

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值