首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2流程小结

2012-11-06 
Struts2流程总结Struts 流程总结1、Web.xml:filterfilter-namestruts2/filter-namefilter-classorg

Struts2流程总结
Struts 流程总结
1、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>

2、前台页面:
在前台页面中使用Struts2的标签库,基本操作是导入标签库需要的文件,然后可以使用标签,操作如下:
   <%@ taglib prefix="s" uri="/struts-tags" %>

    <s:form action="pointConverter">
        <s:textfield name="point.x" label="x"></s:textfield>
   <s:textfield name="point.y" label="y"></s:textfield>
   <s:textfield name="age" label="age"></s:textfield>
   <s:textfield name="username" label="username"></s:textfield>
   <s:textfield name="date" label="birthday"></s:textfield>
   <s:submit label="submit"></s:submit>
    </s:form>
  
   如果使用普通的标签进行表单的书写,那么在表单中action属性后面格式需要是.action。本例中表示为pointConverter.action。如果使用

Struts2标签可以只写入pointConverter。

3、配置文件:
strtus.xml
<struts>
<package name="struts2" extends="struts-default">
<action name="pointConverter" class="com.test.action.PointAction">
<result name="success">/output.jsp</result>
<result name="input">/input.jsp</result>
</action>
</package>

</struts>

4、Action: 进入到Action之后会首先调用validate()方法进行数据的校验,然后再调用execute()方法。如果不写validate()方法,则直接调

用execute()方法,execute方法中返回可以调用常量SUCCESS、ERROR、INPUT等,只要在配置文件中配置为success、error 、input即可,
  package com.test.action;

import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class PointAction extends ActionSupport
{
private String username;

private String password;

private String repassword;

private int age;

private Date birthday;

private Date graduation;

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 Date getGraduation()
{
return graduation;
}

public void setGraduation(Date graduation)
{
this.graduation = graduation;
}

@Override
public String execute() throws Exception
{
return SUCCESS;
}

@Override
public void validate()
{
System.out.println("validate~~~~~~~~~~~~~~~~~~~");

if(null == username || username.length() < 6 || username.length() > 10)
{
this.addFieldError("username","username invalid");
}
if(null == password || password.length() < 6 || password.length() > 10)
{
this.addFieldError("password","password invalid");
}
else if(null == repassword || repassword.length() < 6 || repassword.length() > 10)
{
this.addFieldError("repassword","re-password invalid");
}
else if(!password.equals(repassword))
{
this.addFieldError("password","two passwords not the same");
}
if(age < 1 || age > 150)
{
this.addFieldError("age","age invalid");
}
if(null == birthday)
{
this.addFieldError("birthday","birthday invalid");
}
if(null == graduation)
{
this.addFieldError("graduation","graduation invalid");
}
if(null != birthday && null != graduation)
{
Calendar c1 = Calendar.getInstance();
c1.setTime(birthday);

Calendar c2 = Calendar.getInstance();
c2.setTime(graduation);

if(!c1.before(c2))
{
this.addFieldError("birthday","birthday should be before graduation");
}
}

}
}
本文章没有涉及到数据类型转换和数据校验等内容,只是简单的阐述Struts2的基本流程。

热点排行