一个action里面不只会调用一个方法,肯定会用到其他的方法,也写在同一个action里面。
  这里不重点讲解了,直接上代码
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.i18n.encoding" value="gb2312" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index" class="com.sun.action.MainPage" method="init">
<result name="yes">
/WEB-INF/index.jsp
</result>
</action>
</package>
<!-- Add packages here -->
</struts>
  然后action  MainPage.java
package com.sun.action;
import com.opensymphony.xwork2.ActionSupport;
public class MainPage extends ActionSupport{
//这个是默认的,如果action里面不配置,这个是默认的
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("hello world");
return "yes";
}
public String init() throws Exception{
System.out.println("hello world 2");
return "yes";
}
}
  通过跑链接 http://localhost:8080/Struts2/index
  能够得出需要的页面