1.框架简介
采用Struts能开发出基于MVC(Model-View-Controller)设计模式的应用构架,用于快速开发Java Web应用。Struts实现的重点在C(Controller),包括ActionServlet/RequestProcessor和我们定制的Action,也为V(View)提供了一系列定制标签(Custom Tag)。但Struts几乎没有涉及M(Model),所以Struts可以采用JAVA实现的任何形式的商业逻辑。。目前为止,有Struts1和Struts2,用户可以根据自己的需求进行选择。
2.所需的lib包
可以去网上下载所有Struts需要的包,我这边是基于1.2的。有以下的包:antlr.jar,commons-beanutils.jar,commons-digester.jar,commons-fileupload.jar,commons-logging.jar,commons-validator.jar,freemarker-2.3.8.jar,jakarta-oro.jar,jstl.jar,ognl-2.6.11.jar,standard.jar,struts.jar,xalan-2.4.0.jar,xwork-2.0.4.jar。
把这些包放置到WebRoot/WEB-INF/lib下面.
3.创建Struts-config.xml.
4.在WEB-INF/web.xml中配置
5.创建Form
package com.babybus.sdteam.vo; import org.apache.struts.action.ActionForm; public class User extends ActionForm { /** * 默认序列化ID */ private static final long serialVersionUID = 1L; // 用户名 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; } }
6.创建ActionForm
package com.babybus.sdteam.action; import java.util.List; 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.babybus.sdteam.bo.ManageServlet; import com.babybus.sdteam.vo.Student; import com.babybus.sdteam.vo.User; public class LoginAction extends Action{ /** * 序列号 */ private static final long serialVersionUID = 1L; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = "success"; User user = (User)form; // 用户名 String userName = user.getUsername(); // 密码 String passWord = user.getPassword(); // 设置属性 request.setAttribute("userName", userName); ManageServlet manageservlet = new ManageServlet(); // 获取全部列表 List<Student> resultlist = manageservlet.getStudentByCondition(null); // 设置List request.setAttribute("students", resultlist); return mapping.findForward(path); } }
7.配置Struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="User" type="com.babybus.sdteam.vo.User"> </form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/login" type="com.babybus.sdteam.action.LoginAction" name="User" scope="request"> <forward name="success" path="/UserList.jsp" /> </action> </action-mappings> <message-resources parameter="com.babybus.sdteam.struts.ApplicationResources" /> </struts-config>
8.最后看下页面代码,配置Form的Action
<form action="login.do" method="post" style="height: 100%"> <table width="100%" height="100%" cellpadding="0" cellspacing="0" > <tr> <td align="center" valign="middle" /> <table> <tr> <td>用户名:</td> <td><input type="text" id = "username" name="username"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" id = "password" /></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="登录"/> <input type="reset" value="重置"/> </td> </tr> </table> </td> </tr> </table> </form>
9.配置到此就结了,更复杂的进一步了解。
结语
- 受益,掌握了Struts1的初级应用
本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士)
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4608474.html