一.
下载
struts
2.0.1
http://struts.apache.org/downloads.html,下载struts-
2.0.1
二.试用struts
2.0.1
1. 新建一个WEBproject,将struts-
2.0.1
改动WEB-INF下的web.xml文件,添�例如以下内容:

<
filter
>

<
filter-name
>
struts2
</
filter-name
>

<
filter-class
>


</
filter-class
>

</
filter
>

<
filter-mapping
>

<
filter-name
>
struts2
</
filter-name
>

<
url-pattern
>
/*
</
url-pattern
>

</
filter-mapping
>

<!–
这里是设置struts2标签,也能够不用设置,由于在struts-core.jar的META-INF文件夹下已经包括了

–>

<
jsp-config
>

<
taglib
>

<
taglib-uri
>
/s
</
taglib-uri
>

<
taglib-location
>


</
taglib-location
>

</
taglib
>

</
jsp-config
>
在web.xml中定义了一个struts2的FilterDispathcer的filter,这个FilterDispatcher用来初始化struts2而且处理全部的WEB请求。
2. 新建一个登录页面login.jsp:


<%
@ page language=“java“ contentType=“text/html; charset=UTF-8“

%>


<%
@taglib prefix=“s“ uri=“/struts-tags“
%>

<
html
>

<
head
>

<
title
>
登录页面
</
title
>

</
head
>

<
body
>

<
s:form
action
=”login”
>

<
table
align
=”center”
>

<
caption
><
h3
>
用户登录
</
h3
></
caption
>

<
tr
>

<
td
><
s:textfield
label
=”用户名”
name
=”username”
/></
td
>

</
tr
>

<
tr
>

<
td
><
s:password
label
=”密 码”
name
=”password”
/></
td
>

</
tr
>

<
tr
align
=”center”
>

<
td
><
input
type
=”submit”
value
=”登录”
/></
td
><
td
><
input
type
=”reset”
value
=”重填”
/></
td
>

</
tr
>

</
table
>

</
s:form
>

</
body
>

</
html
>

3.编写Action login:

package
org.rainlife.struts2.action;


import
com.opensymphony.xwork2.ActionSupport;



public
class
LoginAction
extends
ActionSupport
{







































在这个LoginAction类中,继承了ActionSupport。ActionSupport 是xwork2这个开源框架中的一个让action可以更加高速地工作的基类,它包括了action中很多可选服务的默认实现,可以让我们更加easy地自己定义一个action。
在这里我们定义了username和password两个属性并提供了对应的get/set方法。而且定义了一个execute()方法,该方法覆盖ActionSupport类中的execute()方法,能够看到,它仅仅是简单地返回一个字符串(”SUCCESS”或”INPUT”,而不像是在struts1中的返回一个ActionForward,这两个字符串也是在ActionSupport中定义的,在ActionSupport中定义了四个String属性,分别为SUCCESS,INPUT,ERROR,LOGIN。
这样,我们的action就已经完毕了,但还存在一个问题,怎么样让struts2知道我们这个自己定义的action,而且能够在HTML/JSP页面中将动作提交给action呢?答案是须要配置struts.xml文件。
4.配置struts.xml

<?
xml version=”1.0″ encoding=”UTF-8″
?>

<!
DOCTYPE struts PUBLIC


>

<
struts
>

<
package
name
=”struts2″
extends
=”struts-default”
>

<
action
name
=”login”
class
=”org.rainlife.struts2.action.LoginAction”
>

<
result
name
=”error”
>
/error.jsp
</
result
>

<
result
name
=”success”
>
/success.jsp
</
result
>

</
action
>

</
package
>

</
struts
>

在这个struts.xml配置文件里,能够发现和曾经的struts-config.xml已经全然不一样了,而在webwork的配置文件很类似。在这里,我们定义一个名name=”login”的action,通过class属性指向了刚才我们创建的LoginAction类,这样,就将我们定义的action告诉给了struts2。而在HTML/JSP页面中,能够通过这个”login”这个name来将动作提交给对应的Action。
假设在package中设置了namespace属性,如namespace=”/struts2”,则在JSP页面中应该将Form的action设置为”/struts2/login.action”。
5.创建error.jsp和success.jsp:
error.jsp


<%
@ page language=“java“ import=“java.util.*“ pageEncoding=“UTF-8“
%>

<
html
>

<
head
>

<
title
>
错误页面
</
title
>

</
head
>

<
body
>


</
body
>

</
html
>

success.jsp


<%
@ page language=“java“ import=“java.util.*“ pageEncoding=“UTF-8“
%>

<
html
>

<
head
>

<
title
>
成功页面
</
title
>

</
head
>

<
body
>


</
body
>

</
html
>

Struts2会依据在LoginAction中返回的字符串(ERROR或SUCCESS)来和在struts.xml中<result>中的name属性所定义的字符串匹配,并跳转到对应页面。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119025.html原文链接:https://javaforall.net