先看JSP,有两种调用方法,一种是静态的调用,一种是动态的调用。这里我们推荐的是动态调用,因为静态的调用会产生很多的Action
<%@ 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>
Action执行的时候并不一定要执行execute方法<br />
可以在配置文件中配置Action的时候用method=来指定执行哪个方法
也可以在url地址中动态指定(动态方法调用DMI)(推荐)<br />
<a href="<%=basePath %>user/userAdd">添加用户</a>
<br />
<a href="<%=basePath %>user/user!add">添加用户</a>
<br />
前者会产生太多的action,所以不推荐使用
</body>
</html>
struts.xml的配置,用于比较有两个Action,前者是静态调用方法,后者是动态调用方法
<?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.ognl.allowStaticMethodAccess" value="true"></constant>
<package name="user" namespace="/user" extends="struts-default">
<action name="userAdd" class="com.lbx.action.UserAction" method="add">
<result name="success">
/user_add_success.jsp
</result>
</action>
<action name="user" class="com.lbx.action.UserAction">
<result>/user_add_success.jsp</result>
</action>
</package>
</struts>
Action中代码
package com.lbx.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
public String add(){
return SUCCESS;
}
}