本文主要是介绍Struts1.x的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用MyEclipse新建一个Web项目,在MyEclipse菜单中导入框架如下图:
添加框架支持以后如下图:
Struts1.x的核心重点是:2个配置文件,3个类
1,两个配置文件:web.xml,struts-config.xml
2,ActionForm,Action编写,ActionServlet的配置
3,ActionServlet在web.xml中配置,并且框架会自动实例化ActionServlet,不需要编写ActionServlet类
在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="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name /><servlet><!--利用ActionServlet类,框架会自动实例化ActionServlet,对象名为action--><servlet-name>action</servlet-name><servlet-class>org.apache.struts.action.ActionServlet</servlet-class><!-- ActionServlet读取配置文件的路径 --><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><!-- 表示应用启动, ActionServlet就载入实例化--><load-on-startup>0</load-on-startup></servlet><!--访问名为action的Servlet,也就是 org.apache.struts.action.ActionServlet,要添加的扩展名是“.do”,ActionServlet的映射路径--><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>
编写JSP:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><body><!-- 按此地址提交给ActionServlet时, ActionServlet将地址的项目名称和扩展名.do去掉,变为/login,读取配置文件,找到要提交的Action对象--><form action="/StrutsTest/login.do" method="post">请输入账号:<input name="account" type="text" ><br>请输入密码:<input name="password" type="password" > <input type="submit" value="登录"> </form></body>
</html>
图示:
编写配置ActionForm:
ActionServlet将表单信息封装在ActionForm中,转交给Action.需要编写ActionForm来容纳表单里的数据。
1,必须继承ActionForm接口
2,ActionForm内必须编写和表单元素同名的属性
package com.strutstest;import org.apache.struts.action.ActionForm;public class LoginForm extends ActionForm{private String account;private String password;public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
编写配置Action
Strust框架中,将ActionForm转交给Action来处理,Action负责业务逻辑处理
1,必须继承Action接口
2,必须重写execute方法,重写execute方法,是因为Action接受数据后由ActionServlet自动调用它的execute方法,这个方法的运行通过反射机制进行。
package com.strutstest;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{//mapping的作用是访问配置文件,ActionForward封装跳转的目标路径public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{LoginForm loginform=(LoginForm)form;String account=loginform.getAccount();String password=loginform.getPassword();String url=null;if(account.equals(password)){return new ActionForward("/Success.jsp");}return new ActionForward("/Fail.jsp");}
}
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要在此配置文件中注册, 注册ActionForm --><form-beans ><form-bean name="LoginForm" type="com.strutstest.LoginForm"></form-bean></form-beans><global-exceptions /><global-forwards /><!-- 注册Action ,ActionServlet根据此path找到要提交的Action,此元素name属性要和<form-beans>的name对应--><action-mappings ><action path="/login" name="LoginForm" type="com.strutstest.LoginAction"></action></action-mappings><message-resources parameter="com.strutstest.ApplicationResources" />
</struts-config>
loginSucess.jsp和loginFail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><body>登录成功!</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><body>登录失败!</body>
</html>
程序的运行流程:
1,login.jsp中的表单提交到地址为"/StrutsTest/login.do",提交给ActionServlet,ActionServlet把提交的地址的项目路径和扩展名.do去掉,变为"/login",读取配置文件
2,在配置文件中,根据/login,找到配置文件中action对应的类,从而得到提交的LoginAction,通过LoginAction的name属性找到LoginForm,找到对应的LoginForm类
3,将表单封装在一个Login对象中,提交给LoginAction
4,ActionServlet调用Action的execute方法,处理后返回一个ActionForward对象给ActionServlet
5,ActionServlet跳转到相应页面。
这篇关于Struts1.x的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!