struts2 调用默认方法execute()的事例

1.准备

开发工具:MyEclipse

运行环境:Tomcat

2.创建web工程

3.导入struts2的核心jar包

4.定义web.xml文件中的struts2过滤器,这里在导入struts2是已经帮我们写好了。如果没有写,需要我们自己定义

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!-- struts2过滤器 -->
	<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>
5.写一个简单的登录测试Action,里面包含execute()方法

UsersAction.java

package cn.icloudit.web.action;

public class UsersAction {
	private String uname;
	private String upwd;

	public String execute() {
		if("tom".equals(uname)){
			return "success";
		}
		return "login";
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUpwd() {
		return upwd;
	}

	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}

}
6.定义struts.xml配置文件

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="users" namespace="/" extends="struts-default">
		<action name="login" class="cn.icloudit.web.action.UsersAction">
			<result name="success">
				/main.jsp
			</result>
			<result name="login">
				/index.jsp
			</result>
		</action>
	</package>
</struts>    
在上面的红色部分,可以看到我并没有写 method = "execute",
也就没有指定返回的方法。(注意到这一点非常重要)


7.编写视图页面
/struts2_s2/WebRoot/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <div align="center">
    	<form action="/struts2_s2/login" method="post"> 
    	用户名:<input type="text" name="uname"><br/>
    	密    码:<input type="password" name="upwd"><br/>
    	<input type="submit" value="登录">
    	</form>
    </div>
  </body>
</html>

form表单提交到struts2_s2项目下的login,在struts.xml中定义好了的


/struts2_s2/WebRoot/main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    
    <title>My JSP 'main.jsp' starting page</title>
    


  </head>
  
  <body>
    登录成功,欢迎${uname } <br>
  </body>
</html>

8.启动tomcat,运行测试。



登录成功,跳转页面

测试成功!


9.总结:
关键的地方在UsersAction.java中,定义了一个execute()方法,

在struts.xml配置的action中,可以不用指定method="execute",

依然会在action指定的类

class="cn.icloudit.web.action.UsersAction"
中默认访问execute()方法,

这就是struts2的execute()方法的使用。


要注意,如果UsersAction中有其他方法要使用,还需要指定method="方法名”,当方法太多时,可以在action中使用通配符。如

<action name="*" class="cn.icloudit.web.action.UsersAction" method="{1}">

如果Action定义太多时,在每个不同的Action中有一些xiangtong在实际开发中常常会遇到这种情况,这是需要定义包的命名空间

<package name="users" namespace="/users" extends="struts-default">



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值