struts 2.0 hello world demo

1. 在 web.xml 加入以下代码:

<filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
 
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <!-- 使用该核心Filter来接受所有的Web请求 -->
      <url-pattern>/*</url-pattern>
  </filter-mapping>


2. 在scr 目录下创建名为struts 的xml 文件:

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>
    <include file="/struts-basic.xml"></include>
    <include file="/com/itcast/test/config/test-struts.xml"></include>
</struts>

3.对struts.xml进行扩展,struts-basic.xml存放一些基本配置信息(如:异常处理), *-struts.xml存放具体的处理信息:

struts-basic.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>
    <package name="basic" extends="struts-default">
        <global-results>
            <result name="exception">/jsp/common/exception.jsp</result>
        </global-results>
        
        <global-exception-mappings>
            <exception-mapping result="exception" exception="java.lang.RuntimeException"></exception-mapping>
        </global-exception-mappings>
    </package>
</struts>

test-struts.xml

<struts>
    <!-- Action必须放在指定的包名空间中-->
    <package name="test" extends="basic" namespace="/test">
        <action name="login" class="com.itcast.test.action.LoginAction" method="execute">
            <result name="TEST_SUCCESS">/jsp/test/test2.jsp</result>    
        </action>
    </package>
</struts>


4.创建具体的处理文件:

LoginAction.java

package com.itcast.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    @Override
    public String execute() throws Exception {
        return "TEST_SUCCESS";
    }    
}


5. jsp 文件:

test1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>

    <a href="http://localhost:8088/alex_shop2/test/login.action">login</a>

</body>
</html>

test2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test2</title>
</head>
<body>
    hello world
</body>
</html>


页面流为: test1.jsp -> click "login" -> test2.jsp(show "hello world")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值