struts2教程1
来源:互联网 发布:程序员 交易员 编辑:程序博客网 时间:2024/04/27 18:23
1.从apache官网下载struts2(v2.2.1)的压缩文件,解压缩
2.打开myeclipse(v8.6),将struts2解压缩后lib下的ongl,strtus2-core,xwork-core,commons-logging,freemaker,javassist(该包需要另外下载,老版本里不需要)这六个必须包导入
在struts2(v2.2.3.1)除了上面包外还需导入commons-io,commons-lang,commons-fileupload
3.配置web.xml(WEB-INF下)
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<
web-app
version
=
"2.5"
03
xmlns
=
"http://java.sun.com/xml/ns/javaee"
04
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
05
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
06
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
07
<
filter
>
08
<
filter-name
>struts2</
filter-name
>
09
<
filter-class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</
filter-class
>
10
</
filter
>
11
12
<
filter-mapping
>
13
<
filter-name
>struts2</
filter-name
>
14
<
url-pattern
>/*</
url-pattern
>
15
</
filter-mapping
>
16
17
18
<
welcome-file-list
>
19
<
welcome-file
>index.jsp</
welcome-file
>
20
</
welcome-file-list
>
21
</
web-app
>
老版本中用的过滤器是Dispatch类
4.编写用户登录表单
01
<%@ page language=
"java"
import
=
"java.util.*"
pageEncoding=
"utf-8"
%>
02
<%
03
String path = request.getContextPath();
04
String basePath = request.getScheme()+
"://"
+request.getServerName()+
":"
+request.getServerPort()+path+
"/"
;
05
%>
06
07
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
>
08
<html>
09
<head>
10
<base href=
"<%=basePath%>"
>
11
12
<title>My JSP
'index.jsp'
starting page</title>
13
<meta http-equiv=
"pragma"
content=
"no-cache"
>
14
<meta http-equiv=
"cache-control"
content=
"no-cache"
>
15
<meta http-equiv=
"expires"
content=
"0"
>
16
<meta http-equiv=
"keywords"
content=
"keyword1,keyword2,keyword3"
>
17
<meta http-equiv=
"description"
content=
"This is my page"
>
18
<!--
19
<link rel=
"stylesheet"
type=
"text/css"
href=
"styles.css"
>
20
-->
21
</head>
22
23
<body>
24
<form action=
"Login"
method=
"post"
>
25
26
<Table>
27
<Tr>
28
<td>用户名:</Td>
29
<td><input type=
"text"
name=
"username"
/></Td>
30
</Tr>
31
<Tr>
32
<td>密码:</Td>
33
<td><input type=
"password"
name=
"password"
/></Td>
34
</Tr>
35
<Tr>
36
<td><input type=
"submit"
name=
"submit"
value=
"提交"
/></Td>
37
<td><input type=
"reset"
name=
"reset"
value=
"重置"
/></Td>
38
</Tr>
39
</Table>
40
41
42
</form>
43
</body>
44
</html>
5.编写LoginAction.java
01
package
com.struts2.action;
02
03
public
class
LoginAction {
04
private
String username;
05
private
String password;
06
07
public
String execute()
throws
Exception
08
{
09
if
(
this
.getUsername().equals(
"tom"
)&&
this
.getPassword().equals(
"111111"
)){
10
return
"success"
;
11
}
else
{
12
return
"error"
;
13
}
14
}
15
16
public
String getUsername() {
17
return
username;
18
}
19
20
public
void
setUsername(String username) {
21
this
.username = username;
22
}
23
24
public
String getPassword() {
25
return
password;
26
}
27
28
public
void
setPassword(String password) {
29
this
.password = password;
30
}
31
32
33
34
}
6.配置strtus.xml(src下)
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<!DOCTYPE struts PUBLIC
03
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04
"http://struts.apache.org/dtds/struts-2.0.dtd">
05
06
<
struts
>
07
08
<
package
name
=
"strutsqs"
extends
=
"struts-default"
>
09
<
action
name
=
"Login"
class
=
"com.struts2.action.LoginAction"
>
10
<
result
name
=
"success"
>/welcome.jsp</
result
>
11
<
result
name
=
"error"
>/error.jsp</
result
>
12
</
action
>
13
</
package
>
14
</
struts
>
7.结果页面就一句话,就不写了
- struts2-教程学习1
- struts2教程1
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- Struts2教程1:第一个Struts2程序
- jquery 实现各行换颜色
- hadoop,hbase,zookeeper错误日志及部分解决办法
- vba office 2003 全套 中文帮助文档 手册
- 字体编辑用中日韩汉字Unicode编码表
- 详解web.xml中对env-entry元素的处理方法
- struts2教程1
- JS 实现php中的 BASE64_ENCODE 和 BASE64_DECODE
- JAVA ANNOTATION详解
- PostgreSQL启动过程中的那些事十九:walwriter进程一
- 地图代码
- wizard解析错误
- (floyd+最小路径覆盖) Treasure Exploration (P2594)
- drupal中展现一个jquery进度条的效果
- f1