HelloWorld 你让我情何以堪- -!

想学习下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>
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值