struts开发<struts中的action详细配置. 二>

时间:2023-07-26 22:24:44
struts开发&lt;struts中的action详细配置. 二&gt;

在eclipse中新建项目StrutsDemo1【struts的配置见】struts开发<在eclipse中配置struts. 一>

详细文件夹结构例如以下

struts开发&lt;struts中的action详细配置. 二&gt;


第一种配置方法

新建UserAction

package fzl.user.struts.demo;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
public String list(){ System.out.println("list");
return "success";
}
public String input(){
System.out.println("input");
return "success";
} public String add(){ System.out.println("add");
return "success";
}}

在struts重配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <!-- 第一种,配置多个action -->
<action name="add" class="fzl.user.struts.demo.UserAction" method="add">
<result name="success">/WEB-INF/User/add.jsp</result>
</action>
<action name="input" class="fzl.user.struts.demo.UserAction" method="input">
<result name="success">/WEB-INF/User/input.jsp</result>
</action>
<action name="list" class="fzl.user.struts.demo.UserAction" method="list">
<result name="success">/WEB-INF/User/list.jsp</result>
</action> </package> </struts>

新建input.jsp 、list.jsp、add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>------------------input-----------------</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>------------------add-----------------</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>------------------list-----------------</h1>
</body>
</html>

执行tomcat,在浏览器输入http://localhost:9000/strustDemo1/input

struts开发&lt;struts中的action详细配置. 二&gt;

同理输入响应的文件夹就会得到对应的页面输出


另外一种配置方法

其它的不用修改,直接修改UserAction和struts.xml就可以

package fzl.user.struts.demo;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
public String list(){ System.out.println("list");
return "list";
}
public String input(){
System.out.println("input");
return "input";
} public String add(){ System.out.println("add");
return "add";
}}
<action name="User" class="fzl.struts.demo.UserAction">
<result name="add">/WEB-INF/User/list.jsp</result>
<result name="input">/WEB-INF/User/input.jsp</result>
<result name="list">/WEB-INF/User/add.jsp</result>
</action>

执行时输入http://localhost:9000/StrutsDemo2/User!add 就可以得到对应的页面


第三种方法

package fzl.user.struts.demo;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
public String list(){ System.out.println("list");
return "success";
}
public String input(){
System.out.println("input");
return "success";
} public String add(){ System.out.println("add");
return "success";
}}

通过通配符配置,通配符不来了解的自行百度

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="*_*" class="fzl.user.struts.demo.{1}Action"
method="{2}">
<result>/WEB-INF/{1}/{2}.jsp</result>
</action> </package> </struts>

struts开发&lt;struts中的action详细配置. 二&gt;

action的配置方法大概就这三种,后两种在开发中用的比較多