Struts基础开发

MVC与Struts中各个组件的对应关系:

NO.组成部分传统MVCStruts
1视图(View)JSP(可以加入JSTL减少页面代码)在传统页面中提供了标签库的支持
2控制器(Controller)ServletAction
3模型(Model)JavaBeanActionForm、JavaBean
配置Struts开发环境

1、下载struts.zip开发包,将解压缩文件中lib中的*.jar包复制到项目lib中

2、建立Struts核心配置文件:struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	<form-beans/>
	<global-exceptions />
	<global-forwards />
	<action-mappings/>
	<message-resources parameter="org.lxh.struts.ApplicationResources" />
</struts-config>
struts-config.xml文件中首先需要配置<struts-config>这个节点,在此节点下有如下的子节点:

<form-beans/>:用于配置ActionForm。
<global-exceptions />:用于配置全局异常。
<global-forwards />:用于配置全局跳转。
<action-mappings/>:用于配制Action。
<message-resources>:用于配置资源文件路径,资源文件保存在WEB-INF\classes文件夹中,通过parameter属性指定路径及文件名称,文件名称的后缀是*.properties。

3、配置web.xml

  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
    	org.apache.struts.action.ActionServlet
    </servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
    <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
此处实际上配置的是一个ActionServlet,此映射路径为*.do。

4、配置web.xml文件,增加标签库配置

<jsp-config>
	<taglib>
		<taglib-uri>http://www.stu.cn/struts/bean</tablib-uri>
		<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>http://www.stu.cn/struts/logic</tablib-uri>
		<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>http://www.stu.cn/struts/html</tablib-uri>
		<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
	</taglib>
</jsp-config>

开发第一个Struts程序

建立页面----hello.jsp

<%@ page language="java" pageEncoding="GBK"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<html:html lang="true">
<head>
	<html:base />
	<title>hello.jsp</title>
</head>
<body>
	<html:errors/>
	<logic:present name="msg" scope="request">
		<h2>${msg}</h2>
	</logic:present>
	<html:form action="hello.do" method="post">
		请输入信息:<html:text property="info"></html:text>
		<html:submit value="显示"></html:submit>
	</html:form>
</body>
</html:html>
建立ActionForm----HelloForm.java

package org.lxh.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class HelloForm extends ActionForm {

	private String info;//接收info提交参数

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {//验证输入数据
		ActionErrors errors = new ActionErrors();
		if (this.info == null || "".equals(this.info)) { // info的输入内容为空
			// 现在应该保存错误信息
			errors.add("info", new ActionMessage("error.info"));
		}
		return errors;
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {

	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}
}
ActionForm类中的方法:


NO.方法类型描述
1public ActionErrors validate(ActionMapping mapping ,HttpServletRequest request)普通输入数据验证,返回ActionErrors 对象,根据input指定的路径跳转到错误显示页
2public void reset(ActionMapping mapping ,HttpServletRequest request)普通重置操作

ActionErrors类中的方法

NO.方法类型描述
1public void add(String property,ActionMessage message)普通增加一个错误信息,第一个参数一般与参数的名称一致
 
ActionMessage类中的方法

NO.方法类型描述
1public ActionMessage(String key)构造设置此错误信息的内容
定义资源信息----ApplicationResource.properties

#输入的信息不能为空
error.info = \u8f93\u5165\u7684\u4fe1\u606f\u4e0d\u80fd\u4e3a\u7a7a\uff01
由于资源文件(*.properties)不支持中文,所以此处需要将中文变为UNICODE编码【可以使用native2ascii.exe命令完成】

定义Action----HelloAction.java

package org.lxh.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 org.lxh.struts.form.HelloForm;

public class HelloAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HelloForm helloForm = (HelloForm) form;// TODO Auto-generated method
		String info = helloForm.getInfo(); // 所有的输入内容从ActionForm取出
		request.setAttribute("msg", info); // 将信息设置在request范围之中
		return mapping.findForward("show"); // 此处返回的是一个映射的路径
	}
}

Action类中的方法

NO.方法类型描述
1public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception普通调用Action执行具体的业务操作,此方法接收ActionForm(由struts-config.xml配置),返回ActionForward的实例,跳转路径在struts-config.xml中配置
2protected void saveErrors(HttpServletRequest request,ActionMessages errors)普通保存错误信息,与ActionFrom中的vailidate()方法的功能类似
3protected void saveMessages(HttpServletRequest request,ActionMessages messages)普通保存错误信息,与ActionForm中的vailidate()方法的功能类似
ActionMapping类中的方法

NO.方法类型描述
1public ActionForward findForward(String  forwardName)普通取得<action>节点中<forward>指定的路径
2public ActionForward getInputForward()普通取得<action>节点中input指定的路径
配置struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
	<form-beans>
		<form-bean name="helloForm"
			type="org.lxh.struts.form.HelloForm" />
	</form-beans>

	<global-exceptions />
	<global-forwards />
	<action-mappings>
		<action attribute="helloForm" input="/hello.jsp"
			name="helloForm" path="/hello" scope="request"
			type="org.lxh.struts.action.HelloAction">
			<forward name="show" path="/hello.jsp"></forward>
		</action>
	</action-mappings>

	<message-resources parameter="org.lxh.struts.ApplicationResources" />
</struts-config>
<form-bean>:表示配置的每一个ActionForm,在此节点中定义了name属性指定此ActionForm的名称,type属性表示的是此ActionForm对应的包类名称。

<action>:表示配置的每一个Action,此节点的属性如下。
attribute、name:指定此Action要使用的ActionForm名称,此名称在<form-bean>标签中配置

input:表示当验证出错(ActionForm不为空)时跳转的错误显示页。

path:此Action对应的路径,此时为hello.do。

scope:此Action的作用范围,有request和session两种设置。

type:此Action对应的包.类名称。

在<action>节点中可以同时定义多个<forward>节点,每一个<forward>节点都表示一个映射的跳转路径,通过ActionMapping类的findForward()方法返回的就是一个映射的路径。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值