Struts1入门实例(简单登录)
现在开始加入公司的核心项目,但由于项目开发比较早,所以使用的技术不是很新潮,前台用的还是struts1。
虽然不是什么新技术,但仍可以从中学到好多东西的。花了一个晚上的时间终于把struts1的流程跑通了。
省略了创建过程及加入jar包的过程,直接进入正题。
首页要在web.xml中添加相应的struts配置:
<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>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
然后在config所对应的目录下添加struts配置文件struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <!-- ActionForm --> <form-beans> <!-- name:名称 type:类路径 --> <form-bean name="loginForm" type="com.login.LoginForm"> </form-bean> </form-beans> <!-- 设置全局URL,必须写在Action前面,这样任何Action都可以使用 --> <global-forwards> <forward name="result" path="/result.jsp" /> </global-forwards> <!-- 注册Action --> <action-mappings> <!-- name:名称 type:类路径 path:客户端(JSP)提交到服务器端时指定的路径(Form表单中的action=”/login.do" --> <action path="/login" type="com.login.LoginAction" name="loginForm" scope="request"> <!-- 局部URL,只有这个action可以使用 --> <forward name="success" path="/success.jsp"></forward> <forward name="error" path="/error.jsp"></forward> </action> </action-mappings> </struts-config>
注:Action和Form的关系是一个Action只能对应一个Form(通过Action的name属性)
而一个Form可以被多个Action调用
LoginAction.java
package com.login; 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; public class LoginAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm lf = (LoginForm) form; String username = lf.getUsername(); String password = lf.getPassword(); String errorInfo = ""; String result = UserManager.getInstance().login(username, password); if (result.equals("success")) { request.setAttribute("username", username); return mapping.findForward("success"); } else if (result.equals("username")) { errorInfo = "密码错误"; } else { errorInfo = "用户名错误"; } request.setAttribute("errorInfo", errorInfo); return mapping.findForward("error"); } }
LoginForm.java
package com.login; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; public class LoginForm extends ActionForm { private String username; private String password; public void reset(ActionMapping mapping, HttpServletRequest request) { System.out.println("--------reset()-------------"); super.reset(mapping, request); } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { System.out.println("--------validate()-------------"); return super.validate(mapping, request); } 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; } }
对我个人来说最重要的是前台jsp页面没有掌握,经常在页面上花费很多时间
在编写JSP页面时要用struts标签,而我经常会忘记
引入jsp标签
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>首页</title> </head> <body> <html:form action="/login.do"> 用户名:<html:text property="username"></html:text><br/> 密码: <html:password property="password"></html:password><br/> <html:submit>submit</html:submit> </html:form> </body> </html>
展示结果(将Action的返回值展示到JSP页面上)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Error</title> </head> <body> <bean:write name="errorInfo" scope="request" /> </body> </html>