在eclipse中新建项目StrutsDemo1【struts的配置见】struts开发<在eclipse中配置struts. 一>
具体目录结构如下
第一种配置方法
新建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
同理输入响应的目录就会得到相应的页面输出
第二种配置方法
其他的不用改动,直接改动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>
action的配置方法大概就这三种,后两种在开发中用的比较多
时间: 2024-11-09 00:41:02