struts2初学习之一

做了两个struts2的项目,其实对struts2框架的了解还仅仅停留在知其然的水平,很多机制都不清楚,现在想好好学习下,并结合项目中的代码做下深入的理解。

一、这里是(纯粹的)struts2正常运行需要的包的最小集合([b][color=red]似乎struts2.1.6和xwork-2.1.3.jar无法兼容[/color][/b]):
1.struts2-core-2.1.6.jar
2.commons-io-1.3.2.jar
3.commons-fileupload-1.2.1.jar
4.freemarker-2.3.13.jar
5.ognl-2.6.11.jar
6.xwork-2.1.2.jar
7.commons-logging-api-1.1.jar

红字部分:发现如果换成xwork-2.1.3.jar的话,调用action会出现如下错误:
com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=java.lang.String, name='logMissingProperties'] in public void com.opensymphony.xwork2.ognl.OgnlValueStack.setLogMissingProperties(java.lang.String)
...
.

二、struts2的配置
1.一个典型的web应用(j2ee)是这样的
+--yourwebappfolder........................你的web应用的根目录
---1.jsp...................................你的jsp呀,html呀,都可以放在这里
---2.jsp
+--otherfolder
---3.jsp
---4.jsp
...
+--images
+--other_static_resourc
...
+--WEB-INF..................................重要,用于放置配置
---web.xml..................................著名的“部署描述文件”必要
+--lib.......................................放置我们的jar包
+--classes...................................你的java class文件,实现业务逻辑
2.给我们的web应用加上struts2
a)上面那个yourwebappfolder/WEB-INF/lib/下面当然就是放置我们在第一部分提到的strut2运行所需要的最小的包集合啦,你可以去伟大的apache站点下载它们。
b)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>
struts2prac</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>[color=red]org.apache.struts2.dispatcher.FilterDispatcher[/color]</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>manning</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

在新的strut2(2.1.3以后,推荐使用这个过滤器org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
c)你需要在你的WEB-INF/classes/下面添加一个struts.xml(文件名必须是这个)文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="false" />
<include file="manning/chapterTwo/struts-chapterTwo.xml"/>
</struts>

这里除了设置些struts的配置属性外,最重要的是包含了action的定义,注意那个include元素,其实就是把包含的多个xml文件引入,这样的好处是你不用把所有的action写在同一个文件里面了。那个struts-chapterTwo.xml的文件在WEB-INF/manning/chapterTwo/下,是这样写的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="chapterTwo" namespace="/chapterTwo" extends="struts-default">
<action name="name">
<result>/chapterTwo/NameCollector.jsp</result>
</action>
<action name="HelloWorld" class="manning.chapterTwo.HelloWorld">
<result name="SUCCESS">/chapterTwo/HelloWorld.jsp</result>
</action>
</package>
</struts>

这里定义了两个“action”。例子是strut2 in action中的。我们不妨看看那个类manning.chapterTwo.HelloWorld是怎么写的:
package manning.chapterTwo;

public class HelloWorld {

private static final String GREETING = "Hello ";

private String name;
private String customGreeting;

public String execute() {
this.setCustomGreeting(GREETING + name);
return "SUCCESS";
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setCustomGreeting(String customGreeting) {
this.customGreeting = customGreeting;
}

public String getCustomGreeting() {
return customGreeting;
}

}



easy吧?注意我们那个很重要的execute方法,返回的参数的意义就是这个:
<result name="SUCCESS">/chapterTwo/HelloWorld.jsp</result>

即我们执行成功的话,就跳转到/chapterTwo/HelloWorld.jsp这个页面。你也可以定义自己的参数,只要result元素的name属性值和execute方法的返回字符串匹配就可以。
我们这个例子的流程如下:
浏览器中访问:
http://localhost:8080/strut2prac/yourwebappcontextname/chapterTwo/name.action
正如struts-chapterTwo.xml定义的,跳转到/chapterTwo/NameCollector.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Name Collector</title>
</head>
<body>
<h4>Enter your name </h4>
<s:form action="HelloWorld">
<s:textfield name="name" label="Your name"/>
<s:submit/>
</s:form>
</body>
</html>

这里的表单中name和action类中的属性name一致,你提交到(HelloWorld.action)后,通过struts2神秘的工作,类中的name就被赋值为你提交的值了。然后跳转到/chapterTwo/HelloWorld.jsp,
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>HelloWorld</title>
</head>
<body>
<h3>Custom Greeting Page</h3>
<h4><s:property value="customGreeting"/></h4>
</body>
</html>

customGreeting这个属性同样和action里面的属性对应,输出了你在类中对字符串的操作。注意这个过程中完全没有类似getParameter这样的显示操作。这就是struts2的重要目的之一。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值