ActionServlet,前端控制器
1、根据相应的规则截取Http请求的URL
2、将Http请求分发到相应的Action处理
ActionForm,相当于实体
1、收集表单数据,将表单数据转换成相应的数据类型
Action,业务层控制器
1、 取得表单数据
2、调用业务逻辑
3、返回转向信息
struts采用servlet和JSP技术实现,基于MVC(模型-视图-控制器)框架
实践步骤
>>搭建环境
>>建立ActionForm
>>建立Action
>>页面访问
*环境搭建
1.导入jar包
下载struts-1.3.8,导入lib中的jar包
2.在WEB-INF下建立struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foudation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans /> <action-mappings> <action path="/aaa" forward="/aaa.jsp"></action> </action-mappings> </struts-config>3.修改web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
1.建一个类,继承ActionForm
package demo; import org.apache.struts.action.ActionForm; public class LoginForm extends ActionForm { private String username=null; private String password=null; 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; } }
<form-beans> <form-bean name="loginForm" type="demo.LoginForm"></form-bean> </form-beans>
1.建立一个类,继承Action
覆盖execute方法:需要强制转型,调用其他模块,根据关键字跳转(关键字参照forward标签中的name属性)
package demo; 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 { // TODO Auto-generated method stub LoginForm loginForm=(LoginForm)form; if(loginForm.getUsername().equals("fan")){ return mapping.findForward("loginSuccess"); } else{ return mapping.findForward("loginFailure"); } } }2.注册,修改配置文件struts-config.xml中的action标签
<action path="/login" type="demo.LoginAction" name="loginForm"> <forward name="loginSuccess" path="/LoginSuccess.jsp"></forward> <forward name="loginFailure" path="/LoginFailure.jsp"></forward> </action>*页面访问
<body> <form action="<%= request.getContextPath() %>/login.do" method="post"> username:<input type="text" name="username" /><br /> password:<input type="password" name="password" /><br /> <input type="submit" value="login" /> </form> </body>