上次刚完成了一个struts 的例子,大家都看到了,那么在上-次中我们到底做了什么呢,我现在为大家说说
先来看看我们的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts_HelloWorld by sakrua</display-name>
<!-- the struts config -->
<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>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- end struts config -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
我们在文件中定义了一个 servlet 我们来看看这个类“org.apache.struts.action.ActionServlet” 这个是struts 的核心类
我们看到它也是从HttpServlet中继成来的,它为struts 完成如下的任务:
- 为到来的请求找到URI
- URI映射到ActionMapping
- 创建或找到相关的ActionMapping 实例
- 如果有ActionFrom创建它
- 创建Action实例
- 最后execute返回ActionForwar
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
接着我们用下面的方法关连到我们的struts-config.xml中
看看我们的重点内容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="helloWorldForm" type="org.helloWorld.struts.form.HelloWorldForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/hello" name="helloWorldForm" type="org.helloWorld.struts.action.HelloWorldAction">
<forward name="success" path="/helloworld/success.jsp"></forward>
<forward name="fail" path="/helloworld/fail.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="org.helloWorld.struts.resource.ApplicationResources"></message-resources>
</struts-config>
<?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">
上面是固定的,但我们可以看到我用的是1.3的dtd文件,也就是说我用的是struts的1.3x
<form-beans>
<form-bean name="helloWorldForm" type="org.helloWorld.struts.form.HelloWorldForm"></form-bean>
</form-beans>
定义一个form-beans,在from-beans中我们可以定义很多的form-bean
还记得我们的actionFrom吧,这里就是它的定义,在工作是struts 会为我们创建它
这样的定义在大多数我情况下已经足够了,但还是有很多的其它的内容的,这个我以后再说了
<action-mappings>
<action path="/hello" name="helloWorldForm" type="org.helloWorld.struts.action.HelloWorldAction">
<forward name="success" path="/helloworld/success.jsp"></forward>
<forward name="fail" path="/helloworld/fail.jsp"></forward>
</action>
</action-mappings>
再下来就是定义一个action了,action都要从struts的类中继承,例如org.apache.struts.action.Action
我们的例子中用的就是这个下面来说一下这个
在action中我们完成业务中的工作,其有一个execute方法,我们要重写这个方法来完成我们想做的事情
例如我们的例子中的
package org.helloWorld.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.helloWorld.struts.form.HelloWorldForm;
/**
* struts helloworld 的action form
* @author sakrua
* email :winds8@gmail.com msn:wind_s8@hotmail.com
* 2007年11月5日
*/
public class HelloWorldAction extends Action {
private static final String FAIL ="fail";
private static final String SUCCESS ="success";
//action中所有的请求都会由这个函数处理
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//从actionForm 中取数据
HelloWorldForm helloWorldForm =(HelloWorldForm)form;
String userName = helloWorldForm.getUserName();
String worldToSay = helloWorldForm.getWorldToSay();
//如果没有协作输入转到错误页面
if(userName.equals("")||worldToSay.equals("")){
return mapping.findForward(FAIL);
}
//转到成功页面
return mapping.findForward(SUCCESS);
}
}
在 <action-mappings>中我们可以定义很多的action
<action path="/hello" name="helloWorldForm" type="org.helloWorld.struts.action.HelloWorldAction">
<forward name="success" path="/helloworld/success.jsp"></forward>
<forward name="fail" path="/helloworld/fail.jsp"></forward>
</action>
请注意的事在Action中定义是name是指向某个ActionFrom的,type才是指向我们的Action类
在Action元素内我们定义了两个
<forward name="success" path="/helloworld/success.jsp"></forward>
<forward name="fail" path="/helloworld/fail.jsp"></forward>
与代码一同看,我们很容易想到这是让我们转到不同的页面中的
//如果没有协作输入转到错误页面
if(userName.equals("")||worldToSay.equals("")){
return mapping.findForward(FAIL);
}
//转到成功页面
return mapping.findForward(SUCCESS);
下面现看看我们的页面吧,与其它的普通的HTML不一样的是我们这里多了struts的标签,来我们看一下例子
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Welcome you to use this demo</title>
</head>
<body>
<html:form action="/hello">
请输入你的用户名:
<html:text property="userName"></html:text><br />
请输入你想说的话:
<html:textarea property="worldToSay"></html:textarea><br />
<html:submit value="submit"></html:submit>
</html:form>
</body>
</html>
这个是index.jsp文件的代码,
我们先来讲讲标签
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
这两句让我们导入了struts 的标签
这样我拉就可以像下面一样使用了
<html:form action="/hello">
请输入你的用户名:
<html:text property="userName"></html:text><br />
请输入你想说的话:
<html:textarea property="worldToSay"></html:textarea><br />
<html:submit value="submit"></html:submit>
</html:form>
也许有些人已经看到了一个东东,那就是标签中的property都与我们的ActionForm中定义的是一样的,
没错,只要这样,在数据提交的时候Struts就会为我们把数据填入Form中
再看回到struts-conf.xml文件
我们可以发现
<message-resources parameter="org.helloWorld.struts.resource.ApplicationResources"></message-resources>
这句话,这句定义了Struts的资源文件在国际化中我们会经常用到,同时在fail.jsp中我们也用到了
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>请输入完整的信息</title>
</head>
<body>
<bean:message key="helloWorld.userNameRequire"/><br />
<bean:message key="helloWorld.worldToSayRequire"/><br />
<a href="index.jsp" >转到转入页面</a>
</body>
</html>
<bean:message key="helloWorld.userNameRequire"/><br />
<bean:message key="helloWorld.worldToSayRequire"/><br />
下面为properties.文件的内容
helloWorld.userNameRequire=请填写用户名
helloWorld.worldToSayRequire=请填写你想说的话
key 与其是对应的
到这里,我们大约看到了struts的一小部分的内容了,在后面我们会继续讲
例子下载
http://winds8.googlepages.com/Struts_HelloWorld.war
http://winds8.googlepages.com/servlet_Test_project.war
posted on 2007-11-08 22:43
风の使者 阅读(142)
评论(0) 编辑 收藏 所属分类:
struts