Struts2框架出来有段时间了,终于开始学习Struts了,也写点东西记录下自己的历程
此文主要来自 www.blogjava.net/Max的实例,自己是初学,自己动手实践真的很有趣,有不对的地方希望大家给有指正。
首先login.jsp页面表单:
<
s:form action
=
"
login
"
method
=
"
post
"
>
<
s:textfield name
=
"
username
"
label
=
"
USERNAME
"
tooltip
=
"
Enter Your UserName
"
></
s:textfield
>
<
s:password name
=
"
password
"
label
=
"
PASSWORD
"
tooltip
=
"
Enter Your PassWord
"
></
s:password
>
<
s:submit label
=
"
login
"
></
s:submit
>
</
s:form
>
struts.xml文件action配置:
<
action
name
="login"
class
="com.bulktree.struts2.LoginAction"
>
<
result
name
="success"
>
/welcome.jsp
</
result
>
<
result
name
="input"
>
/login.jsp
</
result
>
</
action
>
处理表单的LoginAction.java类文件:
package
com.bulktree.struts2;

import
com.opensymphony.xwork2.ActionSupport;


public
class
LoginAction
extends
ActionSupport
{
private String username;
private String password;

public String getUsername()
{
System.out.println("******getUsername()*****");
return username;
}

public void setUsername(String username)
{
System.out.println("*****setUsername()*****");
this.username = username;
}

public String getPassword()
{
System.out.println("******getPassword()*****");
return password;
}

public void setPassword(String password)
{
System.out.println("******setPassword******");
this.password = password;
}
@Override

public String execute() throws Exception
{
System.out.println("*****execute()*****");
return SUCCESS;
}
@Override

public void validate()
{
System.out.println("******validate******");

if(null == username || username.length() < 5)
{
this.addFieldError("username","USERNAME ERROR");
}

if(null == password || password.length() < 5)
{
this.addFieldError("password","PASSWORD ERROR");
}
}
}
显示页面welcome.jsp
YOUR USERNAME:
<
FONT size
=
"
5
"
color
=
"
red
"
><
s:property value
=
"
username
"
/></
FONT
><
br
>
YOUR PASSWORD:
<
FONT size
=
"
5
"
color
=
"
red
"
><
s:property value
=
"
password
"
/></
FONT
><
br
>
控制台输出信息:
******setPassword****** //set赋值操作
*****setUsername()***** //set赋值操作
以上有struts2框架自动执行
******validate****** //表单验证
*****execute()***** //业务逻辑
以上有struts2框架自动执行
******getUsername()***** //get方法得到值返回到welcome.jsp页面
******getPassword()***** //get方法得到值返回到welcome.jsp页面
此文主要来自 www.blogjava.net/Max的实例,自己是初学,自己动手实践真的很有趣,有不对的地方希望大家给有指正。
首先login.jsp页面表单:


















































































客户端输入login.jsp表单传回服务器时action="login" 请求转发到struts.xml 匹配 name="login" class="com.bulktree.struts2.LoginAction" 找到处理请求的LoginAction类,进行自动set方法赋值,赋值后自动的执行 String execute()方法,返回String类型,默认为SUCCESS,(即struts.xml文件里action 的name属性默认为success)根据返回的String的值,决定执行哪个页面
< result name = "success" > /welcome.jsp </ result > “/” 表示绝对路径