Struts2实例创建过程

prepare:从官方网站下载Struts2。本人所用的为struts-2.1.6-all

1、创建一个web工程:KarenTest

2、解压struts2的下载包,在Lib目录下copy 以下5个jar包到KarenTest的lib下.其中commons-fileupload-1.2.1.jar.非加不可,不加则会报错。

    commons-logging-1.0.4.jar,framemarker-2.3.8.jar,ognl-2.6.11.jar,struts-core-2.0.11.jar,xwork-2.0.4.jar,commons-fileupload-1.2.1.jar.

3、新建包com.test.action,在该包下新建一个Action:RegisterAction.继承ActionSupport. 添加属性,并生成get,set方法,重写父类的execute方法。execute方法中如果成功则会默认返回一个success.
package com.test.action;

import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport
{
   private String userid;
   private String username;
   private String password;
   private String repassword;
   private int age;
   private Date birthday;
   private String email;
  
   public String getUserid()
    {
     return userid;
    }
   public void setUserid(String userid)
    {
     this.userid = userid;
    }
   public String getUsername()
    {
     return username;
    }
   public void setUsername(String username)
    {
     this.username = username;
    }
   public String getPassword()
    {
     return password;
    }
   public void setPassword(String password)
    {
     this.password = password;
    }
   public String getRepassword()
    {
     return repassword;
    }
   public void setRepassword(String repassword)
    {
     this.repassword = repassword;
    }
   public int getAge()
    {
     return age;
    }
   public void setAge(int age)
    {
     this.age = age;
    }
   public Date getBirthday()
    {
     return birthday;
    }
   public void setBirthday(Date birthday)
    {
     this.birthday = birthday;
    }
   public String getEmail()
    {
     return email;
    }
   public void setEmail(String email)
    {
     this.email = email;
    }
  
  public String execute() throws Exception
    {
     return SUCCESS;
    }

}

4、修改indext.jsp,加入struts2的标签库引用<%@ taglib prefix="s" uri="/struts-tags" %>
   在body中定义显示内容:其中name为text的name,label是该text前面显示的。label可以任意取。而name是与action中对应的get**中的**相对应。
   <s:form action="register">
   <s:textfield name="username" label="username"></s:textfield>
   <s:password name="password" label="password"></s:password>
   <s:password name="repassword" label="repassword"></s:password>
   <s:textfield name="age" label="age"></s:textfield>
   <s:textfield name="birthday" label="birthday"></s:textfield>
   <s:textfield name="email" label="email"></s:textfield>
   <s:submit label="submit"></s:submit>
   <s:reset label="reset"></s:reset>
</s:form>

5、新建一个成功页面result.jsp.同index.jsp一样要先设置标签库<%@ taglib prefix="s" uri="/struts-tags" %>
同时在body中定义显示的项
    username:<s:property value="username"/>
    password:<s:property value="password"/>
    age:<s:property value="age"/>

6、在web.xml中配置过滤器。

<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

7、在src下建立struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "
http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="struts2" extends="struts-default">

   <action name="register" class="com.test.action.RegisterAction">
    <result name="success">result.jsp</result>
   </action>

</package>

</struts>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值