码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-10-04 13:36:36      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   文件   

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

详细文件夹结构例如以下

bubuko.com,布布扣



第一种配置方法

新建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

bubuko.com,布布扣

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


另外一种配置方法

其它的不用修改,直接修改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>

bubuko.com,布布扣


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


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

标签:style   blog   http   color   io   os   ar   java   文件   

原文地址:http://www.cnblogs.com/zfyouxi/p/4005913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!