struts

 运用struts需要编写web.xml文件,当然是包包struts的包已经引入的情况下,用MyEclipse还可以自动生成。

<?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="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>action</servlet-name> <!--action是个servlet-->
    <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>

在struts-config.xml中设置Form和Action

<?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 ="loginForm" type="com.LoginForm"></form-bean>
  </form-beans>
  <action-mappings>
    <action path="/login" type="com.LoginAction" name="loginForm">
     <forward name="loginsuccess" path="/LoginSuccess.jsp"></forward>
     <forward name="loginfailure" path="/LoginFailure.jsp"></forward>
    </action>
  </action-mappings>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>  <!-- bean中的form的name 要在action中的name对应-->

最后编写两个类分别继承action和actionform ,重写action中的excute方法。

在jsp界面发出login.do的post请求

思考:什么填充form,什么时候填充,根据什么条件填充? Actionservlet如何发请求到action ?Action运行后如何跳转?

An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (RequestProcessor) will select an appropriate Action for each request, create an instance (if necessary), and call the execute method.

Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:

  • Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
  • Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.

When an Action instance is first created, the controller will call setServlet with a non-null argument to identify the servlet instance to which this Action is attached. When the servlet is to be shut down (or restarted), the setServlet method will be called with a null argument, which can be used to clean up any allocated resources in use by this Action.

An ActionForm is a JavaBean optionally associated with one or more ActionMappings. Such a bean will have had its properties initialized from the corresponding request parameters before the corresponding Action.execute method is called.

When the properties of this bean have been populated, but before the execute method of the Action is called, this bean's validate method will be called, which gives the bean a chance to verify that the properties submitted by the user are correct and valid. If this method finds problems, it returns an error messages object that encapsulates those problems, and the controller servlet will return control to the corresponding input form. Otherwise, the validate method returns null, indicating that everything is acceptable and the corresponding Action.execute method should be called.

This class must be subclassed in order to be instantiated. Subclasses should provide property getter and setter methods for all of the bean properties they wish to expose, plus override any of the public or protected methods for which they wish to provide modified functionality.

Because ActionForms are JavaBeans, subclasses should also implement Serializable, as required by the JavaBean specification. Some containers require that an object meet all JavaBean requirements in order to use the introspection API upon which ActionForms rely.

这两个类需要深入理解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值