您的位置:首页 > 编程语言 > Java开发

Struts2学习笔记1

2015-06-05 14:01 239 查看
一.下载struts2.0.1
http://struts.apache.org/downloads.html,下载struts-2.0.1-all.zip,这个压缩包中包括了开发struts2所需的struts2-core.jar核心包以及其他struts2所依赖的JAR文件,另外另一些struts2的演示样例程序以及一些HTML的API文档。

二.试用struts2.0.1

1. 新建一个WEBproject,将struts-2.0.1-all.zip压缩包中的lib文件夹下的全部jar文件复制到WEBproject的/WEB-INF/lib文件夹下。

改动WEB-INF下的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>
<!-- 这里是设置struts2标签,也能够不用设置,由于在struts-core.jar的META-INF文件夹下已经包括了
这个tld文件,J2EE容器会自己主动地载入它 -->
<jsp-config>
<taglib>
<taglib-uri>/s</taglib-uri>
<taglib-location>
/WEB-INF/tlds/struts-tags.tld
</taglib-location>
</taglib>
</jsp-config>

在web.xml中定义了一个struts2的FilterDispathcer的filter,这个FilterDispatcher用来初始化struts2而且处理全部的WEB请求。

2. 新建一个登录页面login.jsp:

<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;

在这个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
"-//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="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

<html>
<head>
<title>错误页面</title>
</head>
<body>
您不能登录!
</body>
</html>

success.jsp

<html>
<head>
<title>成功页面</title>
</head>
<body>
您已经登录!
</body>
</html>

Struts2会依据在LoginAction中返回的字符串(ERROR或SUCCESS)来和在struts.xml中<result>中的name属性所定义的字符串匹配,并跳转到对应页面。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: