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>
第二步:创建相关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 }
第三步:创建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>
第四步:创建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>
第五步:创建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>
注意:1、在配置struts.xml中,写action中最好要加一个method方法,action下面的result是该method方法返回的值,通过此值找到对应的jsp页面
2、写东西时思维不能固话,不能视频上怎么写我就怎么写,不然永远都找不到问题,要按照自己所熟悉的方法去做!!!