14_struts1.x概述

来源:互联网 发布:jquery 判断数组为空 编辑:程序博客网 时间:2024/05/17 23:58

14.1 Struts简介

14.1.1 Struts概述

14.1.2 MVC概述


14.2 第一个struts1.x实例

14.2.1 添加struts特性

项目上右键——MyEclipse——Add Struts。。——修改默认package

添加之后的web.xml

<?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 />    <!-- struts1 -->  <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>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 -->  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

14.2.2 创建HelloAction

New--Other--Struts1.2Form,Action&JSP

14.2.3 完成HelloAction

HelloAction.java

/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package com.uikoo9.struts.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.uikoo9.struts.form.HelloForm;/**  * MyEclipse Struts * Creation date: 01-06-2013 *  * XDoclet definition: * @struts.action path="/hello" name="helloForm" input="/form/hello.jsp" scope="request" validate="true" * @struts.action-forward name="success" path="/form/helloSuccess.jsp" */public class HelloAction extends Action {/* * Generated Methods *//**  * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {HelloForm helloForm = (HelloForm) form;// TODO Auto-generated method stubif(helloForm.getName() == null || helloForm.getName().trim().length() == 0){return mapping.getInputForward();}return mapping.findForward("success");}}

14.2.4 添加输入也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-beans >    <form-bean name="helloForm" type="com.uikoo9.struts.form.HelloForm" />  </form-beans>  <global-exceptions />  <global-forwards />  <action-mappings >    <action      attribute="helloForm"      input="/form/hello.jsp"      name="helloForm"      path="/hello"      scope="request"      type="com.uikoo9.struts.action.HelloAction">      <set-property property="cancellable" value="true" />      <forward name="success" path="/form/helloSuccess.jsp" />    </action>  </action-mappings>  <message-resources parameter="com.uikoo9.struts.ApplicationResources" /></struts-config>

hello.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <head><title>JSP for HelloForm form</title></head><body><html:form action="/hello">name : <html:text property="name"/><html:errors property="name"/><br/><html:submit/><html:cancel/></html:form></body></html>

14.2.5 添加输出页

helloSuccess.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <head><title>JSP for HelloForm form</title></head><body>Hello,${ helloForm.name }. Welcome to struts world.</body></html>


14.3 Struts工作流程

hello.jsp-->hello.do-->struts-config.xml-->HelloAction-->helloSuccess.jsp


14.4 Struts配置文件

14.4.1 Struts配置文件详解

14.4.2 多个struts-config.xml

在web.xml中struts-config.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,/WEB-INF/struts-config2.xml</param-value>    </init-param>  </servlet>   

14.4.3 配置命名空间

如下:

  <servlet>    <servlet-name>action</servlet-name>    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>    <!-- 访问hello.do的时候会使用struts-config.xml -->    <init-param>      <param-name>config</param-name>      <param-value>/WEB-INF/struts-config.xml</param-value>    </init-param>    <!-- 访问namespace1/hello.do的时候会使用 struts-config1.xml -->    <init-param>      <param-name>config/namespace1</param-name>      <param-value>/WEB-INF/struts-config1.xml</param-value>    </init-param>  </servlet>  


14.5 Form Bean实例:提交用户信息

14.5.1 用户信息对应的Form Bean

HelloForm.java

/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package com.uikoo9.struts.form;import org.apache.struts.action.ActionForm;/**  * MyEclipse Struts * Creation date: 01-06-2013 *  * XDoclet definition: * @struts.form name="helloForm" */public class HelloForm extends ActionForm {/* * Generated fields *//** name property */private String name;/* * Generated Methods *//**  * Returns the name. * @return String */public String getName() {return name;}/**  * Set the name. * @param name The name to set */public void setName(String name) {this.name = name;}}

14.5.2 用户信息输入页面

见hello.jsp和helloSuccess.jsp

14.5.3 用户信息校验

在HelloForm中的validate方法中实现

    public ActionErrors validate(ActionMapping mapping,            HttpServletRequest request) {        ActionErrors error = new ActionErrors();                if(name == null || name.trim().length() == 0){            error.add("name", new ActionMessage("hello.error.name"));        }                return error;    }

其中hello.error.name为国际化的资源文件ApplicationResources.properties中的信息

# Resources for parameter 'com.uikoo9.struts.ApplicationResources'# Project strutshello.error.name = \u8BF7\u8F93\u5165\u59D3\u540D

14.5.4 Form Bean中使用Person实体类


14.6 Action的配置

14.6.1 Action与ActionForward配置

14.6.2 把JSP配置为Action

在struts-config.xml中配置如下信息即可通过login.do访问login.jsp

<action path="/login" forward="/form/login.jsp"></action>


14.7 Action实例:保存用户信息到数据库


14.8 Struts1.x的线程安全

14.8.1 Action是线程不安全的

将Action的属性设置为final可以避免

14.8.2 Form Bean是线程安全的


14.9 本章总结

原创粉丝点击