想学习下struts2框架,自然离不开最菜鸟的HelloWorld,悲剧就在于即便对照着参考书抄写代码,硬着头皮几天才显示出那个久违的画面。。。废话少说 无码无真相 总结一下下
web.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <!-- Struts2的核心Filter:名字和实现类 --> <web-app> <display-name>Struts 2.0 Demo</display-name> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <!-- 配置拦截器所要拦截的URL,/*的意思是拦截所有请求 --> <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> </web-app>
拦截器 struts2的核心都在这儿了
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 包的定义,包名和Action类所在的包没有联系 --> <!-- extends定义了包要扩展的包,包tt继承了包struts-default中的拦截器和Action --> <package name="hello" extends="struts-default"> <action name="HelloWorld" class="tutorial.HelloWorld"> <result name="success" >/HelloWorld.jsp</result> </action> <!-- Add your actions here --> </package> </struts>
HelloWorld.java
package tutorial;
import com.opensymphony.xwork2.Action;
public class HelloWorld implements Action {
public static final String MESSAGE = "it is not easy for me to see the page ,how ...";
// action被请求时执行的方法
public String execute() throws Exception {
setMessage(MESSAGE);
// 常量“SUCCESS”是在ActionSupport所实现的Action接口中定义的action的返回结果
return SUCCESS;
}
// 定义消息字符串变量
private String message;
// 设置消息内容
public void setMessage(String message) {
this.message = message;
}
// 取得消息内容
public String getMessage() {
return message;
}
}
HelloWorld.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h2>
<s:property value="message" />
</h2>
</body>
</html>