Struts2 2.5.18爬坑日记

一、.struts2简单入门:

1.创建动态web工程(在创建是可以选择创建web.xml文件):

2.添加struts2的jar包(不用把lib目录下的jar全部导入,导入以下几个就好):

d

3.创建测试类:ActionTest 继承ActionSupport类,并重写execute方法:

package com.lijiancheng.action;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("all")
public class ActionTest extends ActionSupport {
	public String execute() {
		return "sucess";
	}

}

4.配置action的访问路径:

4.1引入dtd约束文件(该dtd文件由struts-core-2.5.18.jar解压得到,并添加到xml catalog):

4.2在src目录下创建struts2的核心配置文件:struts.xml(使用事先引入的约束创建),并配置struts2:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.5.dtd" >
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <package name="hello" extends = "struts-default" namespace="/" 
             strict-method-invocation="false">
    <global-allowed-methods>regex:.*</global-allowed-methods>
        <!-- name:为访问的名称 -->
        <action name="action" class="com.lijiancheng.action.ActionTest" method="execute">
        <!-- name:为返回的值, -->
            <result name="sucess" >hello.jsp</result>
        </action>
    </package>
</struts>

5.在web.xml文件下配置struts2的过滤器:

<!-- 配置struts2过滤器 -->
    <filter>
         <filter-name>struts2</filter-name>
         <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
    </filter>
     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>

6.在webContent目录下创建用于访问的jsp文件(hello.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>Insert title here</title>
</head>
<body>
    <h1>欢迎使用struts2...</h1>
</body>
</html>

6.将项目部署到tomcat下,在浏览器端输入:http://localhost:8080/Hello_Struts2/hello.jsp即可访问:

7.完成,继续下一步的学习。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值