2016/10/29 Action类中execute方法的使用

第一步:先配置web.xml文件

1 <filter>
2          <filter-name>struts2</filter-name>
3          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
4 </filter>
5 <filter-mapping>
6          <filter-name>struts2</filter-name>
7          <url-pattern>/*</url-pattern>
8 </filter-mapping>
web.xml

第二步:创建相关Action.java文件

 1 package com.Test.action;
 2 
 3 public class HelloAction {
 4     String info;
 5 
 6     public String getInfo() {
 7         return info;
 8     }
 9 
10     public void setInfo(String info) {
11         this.info = info;
12     }
13 
14     public String execute() {
15         setInfo("execute執行成功!!!");
16         System.out.println("执行..............");
17         return "success";
18     }
19 }
HelloAction.java

第三步:创建struts.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <!-- 动态方法调用 -->
 7     <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 8     <package name="helloPackage" extends="struts-default">
 9         <action name="helloAction" class="com.Test.action.HelloAction" method="execute">
10             <result name="success">/hello.jsp</result>
11         </action>
12     </package>
13 </struts>
struts.xml

第四步:创建index.jsp文件

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h1>JAVA HELLO WORLD!!!</h1>
11     <h3>class="com.Test.action.HelloAction"</h3>
12     <h4><!-- 动态方法调用 -->constant name="struts.enable.DynamicMethodInvocation" value="true" </h4>
13     <!-- 
14                 宅课学院用法,不起作用
15                 <a href="../helloAction/hello.jsp">..............</a>
16          -->
17     <a href="helloAction!execute">添加</a>
18 </body>
19 </html>  
index.jsp

第五步:创建struts.xml中对应的jsp文件

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@taglib prefix="s" uri="/struts-tags" %>    
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <h1>Hello.jsp</h1>
12 <s:property value="'helloWorld'"/>
13 <s:property value="info"/>
14 </body>
15 </html>
hello.jsp

 注意:1、在配置struts.xml中,写action中最好要加一个method方法,action下面的result是该method方法返回的值,通过此值找到对应的jsp页面

2、写东西时思维不能固话,不能视频上怎么写我就怎么写,不然永远都找不到问题,要按照自己所熟悉的方法去做!!!

转载于:https://www.cnblogs.com/1020182600HENG/p/6011211.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值