struts1的工作原理图:
struts1的工作原理图:
客户端发送请求(Http Request),被struts1的核心控件器ActionServlet接收,ActionServlet根据struts-config.xml里的映射关系找到对就的Action,若找不到就返回500错误到JSP页面。若有就Action里的 excute()方法里执行相应的逻辑操作,比如调用Model层的方法,然后通过ActionForward,跳转到对应的输出页面
struts1的步骤:
1.导入struts1在lib文件下的所有架包;
2.把web-inf文件下的struts-config.xm文件复制到自己项目里面的web-inf下,把注释去掉只留下一对<struts-config>标签;
3.把web-inf文件下的web.xml文件里的servlet配置复制到自己项目里面的web.xml里面;
<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>2</load-on-startup>
</servlet>
4.创建有关的action类继承action重写他的execute方法;
5.根据所写的有关action类配置<struts-config>文件里的东西如:
<action-mappings>
<action path="/addUser"
type="com.wangzhaohui.action.AddUserAction"
>
<forward name="scuess" path="/ok.jsp"></forward>
</action>
</action-mappings>
6.我们还可以写一个继承ActionForm的类,里面的写法和pojo类差不多,看需要接收哪些参数就写什么。配置好了就可来自动接收表单数据
<struts-config>
<form-beans>
<form-bean name="userForm" type="com.wangzhaohui.actionForm.UserActionForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/addUser"
type="com.wangzhaohui.action.AddUserAction"
name="userForm">
<forward name="scuess" path="/ok.jsp"></forward>
</action>
</action-mappings>
</struts-config>
然后在action里面接收数据的时候就可以用如form.getXX来自动接受表单数据了。
(view)视图层里面的.do请求都被actionservlet*控制器(在web.xml里面配置好的)接收到,会读取配置文件(srtuts-config)找到相应的请求,分发给相应的action进行处理。进行处理的时候需要传几个参数:request,response,mapping(把配置拿出来封装成一个对象取出来), 还有一个actionForm(根据配置文件里配置的name=“”来自动接收表单数据,最终调用业逻辑,拿到一些数据返回回来,返回actionservlet的是一个actionForward的跳转信息,通过mapping.findForward找到然后servlet里面它会帮你自动的挑战到相应的页面
1、简介
struts是WEB程序MVC分层架构中的C,属于控制层,主要进行处理用户的请求,基于请求驱动。获取用户的请求地址并将表单中的数据封装到Form对象后交给Action进行处理。在Action中进行条用业务层处理具体的请求后将结果通过ActionMapping封装跳转地址返回给用户。struts是对servlet的再次封装,使得更加灵活高效。下面以一个登录的实例讲解struts的开发过程。
2、在新建的web工程中添加以下struts1.x jar包
antlr-2.7.6.jar、commons-beanutils.jar、commons-digester.jar、commons-fileupload.jar、commons-logging-1.0.4.jar、commons-validator.jar、jakarta-oro.jar、struts.jar。
3、web.xml进行如下配置
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>MyStruts1Prj</display-name>
- <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/conf/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>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <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>
4、新建登陆页面login.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <%
- String basePath = request.getContextPath();
- %>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登陆页面</title>
- </head>
- <body>
- <h1>登陆页面</h1>
- <hr>
- <form action="<%=basePath %>/login.do" method="post" >
- userName:<input id="userName" name="userName" type="text" /><br>
- passWord:<input id="passWord" name="passWord" type="password" /><br>
- <input type="submit" id="submit" name="submit" value="submit" />
- </form>
- </body>
- </html>
5、新建登陆成功后的跳转页面loginSucces.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登陆成功页面</title>
- </head>
- <body>
- <h1>欢迎[<%=request.getAttribute("userName") %>]登陆成功!</h1>
- </body>
- </html>
6、新建登陆失败后的跳转页面loginError.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登陆失败页面</title>
- </head>
- <body>
- <h1>登陆失败!</h1>
- </body>
- </html>
7、新建LoginActionForm.java
- package com.lanp.webapp.form;
- import org.apache.struts.action.ActionForm;
- /**
- * 封装登陆表单数据的FORM类
- * @author LanP
- * @since 2012年4月11日23:07:09
- * @version v1.0
- */
- @SuppressWarnings("serial")
- public class LoginActionForm extends ActionForm {
- 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;
- }
- }
8、新建LoginAction.java
- package com.lanp.webapp.action;
- 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.lanp.webapp.form.LoginActionForm;
- /**
- * 处理登陆的Action类
- * @author LanP
- * @since 2012年4月11日23:07:09
- * @version v1.0
- */
- public class LoginAction extends Action {
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String path = "error";
- LoginActionForm loginActionForm = (LoginActionForm)form;
- String userName = loginActionForm.getUserName();
- String passWord = loginActionForm.getPassWord();
- if(null != userName && "admin".equals(userName) && null != passWord && "admin".equals(passWord)) {
- path = "success";
- request.setAttribute("userName", userName);
- } else {
- path = "error";
- }
- return mapping.findForward(path);
- }
- }
9、配置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>
- <form-beans>
- <form-bean name="loginActionForm" type="com.lanp.webapp.form.LoginActionForm">
- </form-bean>
- </form-beans>
- <action-mappings>
- <action path="/login"
- type="com.lanp.webapp.action.LoginAction"
- name="loginActionForm"
- scope="request">
- <forward name="success" path="/jsp/loginSucces.jsp" />
- <forward name="error" path="/jsp/loginError.jsp" />
- </action>
- </action-mappings>
- </struts-config>