一 先创建一个web工程
二 引入struts的开发包到项目
从 http://struts.apache.org
下载
三 编写login.jsp
<%@
page
language
=
"java"
import
=
"java.util.*"
pageEncoding
=
"ISO-8859-1"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+
"://"
+request.getServerName()+
":"
+request.getServerPort()+path+
"/"
;
%>
DOCTYPE
HTML
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
base
href
=
"
<%=
basePath
%>
"
>
<
title
>
My JSP 'login.jsp' starting page
title
>
<
meta
http-equiv
=
"pragma"
content
=
"no-cache"
>
<
meta
http-equiv
=
"cache-control"
content
=
"no-cache"
>
<
meta
http-equiv
=
"expires"
content
=
"0"
>
<
meta
http-equiv
=
"keywords"
content
=
"keyword1,keyword2,keyword3"
>
<
meta
http-equiv
=
"description"
content
=
"This is my page"
>
head
>
<
body
>
<
form
action
=
"/strutslogin/login.do"
method
=
"post"
>
u:
<
input
type
=
"text"
name
=
"username"
><
br
/>
p:
<
input
type
=
"password"
name
=
"password"
><
br
/>
<
input
type
=
"submit"
value
=
"login"
>
form
>
body
>
html
>
四 编写ActonForm(用户表单)和Action(登录小队长)
package
com.cakin.forms;
//这是一个用户表单,用于填充数据的
import
org.apache.struts.action.ActionForm;
public
class
UserForm
extends
ActionForm {
//定义属性【属性名应该和
jsp
页面的控件名称一致】
//如果有人提出疑问:表单名字是不是一定要和控件名称一样?不一定,但要保证get和set方法的名字和
//控件名称一致
private
String
username
;
private
String
password
;
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;
}
}
package
com.cakin.actions;
//这是一个action(表示小队长,需要继承Action)
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.Action;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
com.cakin.forms.UserForm;
public
class
LoginAction
extends
Action {
//我们需要重新编写一个方法:execute会被自动调用,有点类似
servlet
的service方法。
@Override
public
ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws
Exception {
//把form转成对应的UserForm对象
UserForm userForm=(UserForm)form;
//简单验证
if
(
"123"
.equals(userForm.getPassword())){
return
mapping.findForward(
"ok"
);
}
else
{
return
mapping.findForward(
"err"
);
}
}
}
五 编写struts-config.xml文件,该文件用于配置action actionForm以及对应关系,跳转位置,一般放在/WEB-INF目录下
;
六 写出welcome.jsp页面
<%@
page
language
=
"java"
import
=
"java.util.*"
pageEncoding
=
"ISO-8859-1"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+
"://"
+request.getServerName()+
":"
+request.getServerPort()+path+
"/"
;
%>
DOCTYPE
HTML
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
base
href
=
"
<%=
basePath
%>
"
>
<
title
>
My JSP 'wel.jsp' starting page
title
>
<
meta
http-equiv
=
"pragma"
content
=
"no-cache"
>
<
meta
http-equiv
=
"cache-control"
content
=
"no-cache"
>
<
meta
http-equiv
=
"expires"
content
=
"0"
>
<
meta
http-equiv
=
"keywords"
content
=
"keyword1,keyword2,keyword3"
>
<
meta
http-equiv
=
"description"
content
=
"This is my page"
>
head
>
<
body
>
welcome
<
br
>
body
>
html
>
七 配置web.xml中的ActionServlet
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
xmlns
=
" http://java.sun.com/xml/ns/javaee"
;
xmlns:xsi
=
" http://www.w3.org/2001/XMLSchema-instance"
;
version
=
"2.5"
xsi:schemaLocation
=
" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
;
>
<
display-name
/>
<
servlet
>
<
servlet-name
>
action
servlet-name
>
<
servlet-class
>
org.apache.struts.action.ActionServlet
servlet-class
>
<
init-param
>
<
param-name
>
config
param-name
>
<
param-value
>
/WEB-INF/struts-config.xml
param-value
>
init-param
>
<
init-param
>
<
param-name
>
debug
param-name
>
<
param-value
>
3
param-value
>
init-param
>
<
init-param
>
<
param-name
>
detail
param-name
>
<
param-value
>
3
param-value
>
init-param
>
<
load-on-startup
>
0
load-on-startup
>
servlet
>
<
servlet-mapping
>
<
servlet-name
>
action
servlet-name
>
<
url-pattern
>
*.do
url-pattern
>
servlet-mapping
>
<
welcome-file-list
>
<
welcome-file
>
index.jsp
welcome-file
>
welcome-file-list
>
web-app
>
八 测试