struts 的基本练习

来源:互联网 发布:软件是无形资产 编辑:程序博客网 时间:2024/05/17 05:03

看struts2的时候,方法一是抄书,方法二maven,方法三乱来。

1、struts2就和。net的基本library一样首先是全部引用,必要的库有个列表,类似angel,struts2-core、asm-tree3个asm,common下面的3个,work-core-2,还有free marker。 要放倒web-inf下面的lib,这个就是dll搜索的最先的本地库路径了

2、既然上了struts2,就要通过web 配置告诉系统,filter=struts2,我的配置如何的,我要处理哪些文件(url-pattern)。看我输入错误库的名字后,加载报错找不到class “StrutsPrepareAndExecuteFilter”

first web----webapp名字,

<filter-name>struts2</filter-name>

<!-- <filter-class>org.apache.struts.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

 -->

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>


3、到这是告诉了这个web服务器,struts2配置上了

web-inf是整个网站的root的配置,下面包括lib目录,web。xml。并列的是jsp页面,包括html页面等等。webContent就是网站的root了===webapp就会指向这个root目录的


4、开始整理struts 自身,比如struts.properties 配置字符编码,struts.xml是配置处理filter的 action的信息,action成功/失败后的跳转页面。action要放在一个指定的package 下面,package。action class 作为class的名词

   然后去写这个action class,创建类可选继承自 ActionSupport

import com.opensymphony.xwork2.ActionSupport;

action被触发 执行的函数:execute(),return不同的结果,会导向到配置中的不同的页面,如果 return一个莫名其妙的string呢?

public String execute() throws Exception{

/*

hello="hello word success";

return SUCCESS;

*/

//String com.opensymphony.xwork2.Action.SUCCESS

hello="hello word failed";

return  com.opensymphony.xwork2.Action.ERROR;

}


如果胡乱返回 execute()函数,结果如下:

type Status report

message No result defined for action firstweb.action.TestAction and result 

description The requested resource is not available.


5、 struts2的部分全部准备完毕了,那么要看怎么触发和使用到 action的返回结果了

<%@ taglibprefix="s"uri="/struts-tags"%>   

和很多网页语言一样,要使用后台代码产生的功能,需要再前端引入库, struts-tags来自struts2-core, 里面就有对应的标签了:::

<!-- a 标签 -->

<s:a action="hello">hello</s:a>

<s:form

A 标签,触发action,action的名字叫hello。在url上的体现就是跳转到 hello。action这个url路径   


6、看如何使用 action-execute()的返回的,和5差不多,但是  不直接反应在url上,就是不会出现 success.jsp在url中,而是 被当成内容直接 展示出来。 

    <%@taglib prefix="s"uri="/struts-tags"%>  

    

<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

this is action success result page:::::

<s:property value="hello"/>


这个方法是利用property来传值,如果有多个property,就看 property的name不同 了 ;;多值返回就可以这么用的!


<s:property value="hello"/>

<s:property value="words"/>


http://localhost:8080/firstweb/hello.action












0 0
原创粉丝点击