版权声明:本文为博主原创文章,转载请注明出处 http://blog.csdn.net/u013961718 https://blog.csdn.net/u013961718/article/details/68068068
搭建框架
- 使用myeclipse创建 Java Web 项目StructsDemo
- 右键该项目MyEclipse -> Project Facets[Capabilities] ->Install Apache Struts(1.x) Facet
- WebRoot目录下新建 login.jsp, welcome.jsp, error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="/StrutsDemo/login.do" method="post">
UserName:<input type="text" name="username"/><br><br>
Password:<input type="password" name="password"/><br><br>
<input type="submit" value="Login"/>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome to this page!</h1>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Error Page</title>
</head>
<body>
<h1>This page is Error!</h1>
</body>
</html>
- 打开struts-config.xml文件,该文件可以采用Design和Source两种方式编写,Design模式就是我们自动开发ActionForm和Action的窗口,而Source模式让我们直接编辑源代码。
- 右键,New -> Form。第二个框的username和password一定要和login.jsp表单的name对应。
- 右键,New -> Action
- 拖动login.jsp, welcome.jsp, error.jsp到struts-config.xml的视图上。并使用Connection工具连接。
- 在LoginAction的excute方法中添加代码
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
if("123".equals(userForm.getPassword())){
return mapping.findForward("ok");
}else {
return mapping.findForward("error");
}
}
- 运行