用
Eclipse
开发
Struts
应用
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
一、开发步骤
1、新建web项目
2、为项目添加Struts类库及文件
3、新建FormBean
4、新建Action
5、新建Forward
6、编辑Action
7、部署运行项目
二、新建
web
项目
![](https://i-blog.csdnimg.cn/blog_migrate/e283ca7ad621b82a2abfbbeff33a43c2.png)
三、为项目添加Struts类库及文件
![](https://i-blog.csdnimg.cn/blog_migrate/2b7063168f7dd153f4da3f420bc7c5d1.png)
l Struts conifg path:Struts Config文件的位置
l Struts specification:Struts版本
l ActionServlet name:Action Servlet的名字,默认就可以
l URL patten:用默认*.do
l Base package for new classes:Struts生成的FormBean,Action的包名
l Defautl application resources:指定资源文件ApplicationResource的全名,包括包名
四、新建
FormBean
![](https://i-blog.csdnimg.cn/blog_migrate/5168c7445a5ad5cea47901b8ef085077.png)
l
Name:FormBean的名字
l
Form Impl:FormBean的类型,这里选择Dynamic FormBean,则通过struts-config.xml文件动态帮且我们生成FromBean文件
l
Form Properties:FormBean的属性,这里有username,password两个属性
五、新建
Action
![](https://i-blog.csdnimg.cn/blog_migrate/90e34aa8f54ed3032b2485a23bf1832a.png)
l
use case:一般取完成某功能的单词
l
path:调用当前Action的路径
l
Supperclass:Action的父类
l
Type:Action的全名
l
Form-Name:对应的FormBean名称
l
Form-Scope:FormBean的作用范围
l
Form-Validate Form:是否进行效验
l
Input Source:效验错误后回到哪个页面
六、新建
Forward
![](https://i-blog.csdnimg.cn/blog_migrate/53a35c53f5502c4b0b69240fd6b5308a.png)
l Forward Scope:Forward的作用范围,是全局转向还是局部转向
l Name:Forward的名字
l Path:Forward转向的页面
l Redirect:选择Redirect转向
七、连接Forwad
可以用连线连接起来,如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/43e7414c792f43319e0c06697e79f247.jpeg)
对应的struts-config.xml文件内容
<?
xml
version
="1.0"
encoding
="UTF-8"
?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
< struts-config >
<!-- 对FormBean的定义 -->
< form-beans >
<!-- FormBean的名称和类型,这里是动态FromBean -->
< form-bean name ="loginForm"
type ="org.apache.struts.action.DynaActionForm" >
<!-- FormBean中的属性 -->
< form-property name ="username" type ="java.lang.String" />
< form-property name ="password" type ="java.lang.String" />
</ form-bean >
</ form-beans >
< global-exceptions />
<!-- 以下为两个全局的forward转向 -->
< global-forwards >
< forward name ="success" path ="/form/main.jsp" redirect ="true" />
< forward name ="fail" path ="/form/fail.jsp" redirect ="true" />
</ global-forwards >
<!-- action的定义 -->
< action-mappings >
<!-- attribute:属性 input:对应输入的JSP文件 name:Action的名称
path:Action的访问路径 scope:Action的作用范围 type:Action类的全名 -->
< action attribute ="loginForm" input ="login.jsp" name ="loginForm"
path ="/login" scope ="request"
type ="com.meixin.struts.action.LoginAction" />
</ action-mappings >
< message-resources
parameter ="com.meixin.struts.ApplicationResources" />
</ struts-config >
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
< struts-config >
<!-- 对FormBean的定义 -->
< form-beans >
<!-- FormBean的名称和类型,这里是动态FromBean -->
< form-bean name ="loginForm"
type ="org.apache.struts.action.DynaActionForm" >
<!-- FormBean中的属性 -->
< form-property name ="username" type ="java.lang.String" />
< form-property name ="password" type ="java.lang.String" />
</ form-bean >
</ form-beans >
< global-exceptions />
<!-- 以下为两个全局的forward转向 -->
< global-forwards >
< forward name ="success" path ="/form/main.jsp" redirect ="true" />
< forward name ="fail" path ="/form/fail.jsp" redirect ="true" />
</ global-forwards >
<!-- action的定义 -->
< action-mappings >
<!-- attribute:属性 input:对应输入的JSP文件 name:Action的名称
path:Action的访问路径 scope:Action的作用范围 type:Action类的全名 -->
< action attribute ="loginForm" input ="login.jsp" name ="loginForm"
path ="/login" scope ="request"
type ="com.meixin.struts.action.LoginAction" />
</ action-mappings >
< message-resources
parameter ="com.meixin.struts.ApplicationResources" />
</ struts-config >
八、编辑Action
只需要修改execute()方法
package com.meixin.struts.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 org.apache.struts.action.DynaActionForm;
public class LoginAction extends Action
{
String username = "";
String password = "";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
// 通过loginForm可以自动地得到用户名和密码
DynaActionForm loginForm = (DynaActionForm) form;
// 获取用户名,返回Object类型,需要强制转换
username = (String) loginForm.get("username");
// 获取密码
password = (String) loginForm.get("password");
if (username.equals("meixin") && password.equals("123456"))
{
// 若合法则转向success对应的页面
return mapping.findForward("success");
}
else
{
// 若不合法,则转向fail所对应的页面
return mapping.findForward("fail");
}
}
}
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 org.apache.struts.action.DynaActionForm;
public class LoginAction extends Action
{
String username = "";
String password = "";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
// 通过loginForm可以自动地得到用户名和密码
DynaActionForm loginForm = (DynaActionForm) form;
// 获取用户名,返回Object类型,需要强制转换
username = (String) loginForm.get("username");
// 获取密码
password = (String) loginForm.get("password");
if (username.equals("meixin") && password.equals("123456"))
{
// 若合法则转向success对应的页面
return mapping.findForward("success");
}
else
{
// 若不合法,则转向fail所对应的页面
return mapping.findForward("fail");
}
}
}
九、对应的JSP页面
1、login.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for DynaActionForm form</title>
</head>
<body>
<!-- 这里要输出处理表单的action访问路径 -->
<html:form action="/login.do">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for DynaActionForm form</title>
</head>
<body>
<!-- 这里要输出处理表单的action访问路径 -->
<html:form action="/login.do">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
2、main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'main.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
${username },欢迎您!<br>
</body>
</html>
<%
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 'main.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
${username },欢迎您!<br>
</body>
</html>
3、fail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'fail.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
对不起,你输入的用户名和密码不正确!<a href="form/login.jsp">请重新输出!</a><br>
</body>
</html>
<%
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 'fail.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
对不起,你输入的用户名和密码不正确!<a href="form/login.jsp">请重新输出!</a><br>
</body>
</html>
十、输出结果
1、项目结构
![](https://i-blog.csdnimg.cn/blog_migrate/e2b3463b3c4a48d0f5641a019263158c.png)
2、登录页面
![](https://i-blog.csdnimg.cn/blog_migrate/12ca270f51cdacab5b82bc8b89007752.png)
3、转向后的页面
![](https://i-blog.csdnimg.cn/blog_migrate/5669e37dd103e1bdfa8fb4a9f4be906f.png)
转载于:https://blog.51cto.com/wnight88/140668