struts2的入门程序

一个Struts2的入门程序:

  一:导入Struts2的核心jar文件

  二:在web.xml文件中配置Struts2的中央控制器(过滤器)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts2</display-name>
  
     <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     </filter>
     <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
     </filter-mapping>
     
</web-app>

三:编写Action类

     有三种方式:

     ①一个普通的类,既不实现任何接口,也不继承任何类

     ②继承ActionSupport(最常用)

     ③实现Action接口

package com.edu.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	
	public String login(){
		System.out.println("login====");
		return "success";
	}

	public String rega()throws Exception{
		System.out.println("rega=====");
		return "success";
	} 

	public String show(){
		System.out.println("show=======");
		return "error";
	}
}

四:在struts2的配置文件中配置action

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

  <package name="user" namespace="/" extends="struts-default">
	 
	 <!--
	 	注意:别忘记了在web.xml文件中配置struts的中央过滤器,所有的请求都要经过该过滤器的
	 	          处理才能找到对应的action。
	    {1}:代表action的name(reg_*)中的第一个*号 ,匹配的方法为页面上表单的方法
	 	可以理解为action的name代表一个action类,method代表该类中的一个方法
	 	class代表的是这个action类的全路径。
	 	result中的name为上面方法的返回值,value为如果为该返回值,那么要去的页面
	 -->
	 <action name="reg_*" method="{1}" class="com.edu.action.TestAction">
	  	<result name="success">/success.jsp</result>
	  	<result name="error">/error.jsp</result>
	 </action> 
	 
  </package>

</struts>

五:写相关页面

页面上表单的action的路径为reg_methodName.action。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值