后端开发基础-Struts2框架学习-004—Struts2开发应用练习

演示  include(包含)多个配置文件 的使用

Struts 2自带有“包含文件”功能,包含多个Struts配置文件合并为一个单元。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name="test" namespace="/emp" extends="struts-default">
		<action name="list" class="com.dk.action.EmpListAction">
			<result name="success">
				/WEB-INF/emp_list.jsp
			</result>
		</action>
	</package>
	
	<package name="demo2" namespace="/user" extends="struts-default">
		<!-- 请求toLogin会自动调用框架底层
			默认的Action的execute方法处理,返回"success" -->
		<action name="toLogin">
			<!-- name属性默认为success
				type属性默认为dispatcher -->
			<result>
				/WEB-INF/login.jsp
			</result>
		</action>
		<!-- login.action请求调用
			LoginAction的execute方法 -->
		<action name="login" class="com.dk.action.LoginAction">
			<result name="success">
				/WEB-INF/ok.jsp
			</result>
			<result name="error">
				/WEB-INF/login.jsp
			</result>
		</action>
	</package>
	
	<!-- name唯一性标识
		extends继承框架里的Struts-default包 -->
	<package name="demo1" extends="struts-default">
		<!-- name请求名;class指定Action组件
			method指定Action方法名,默认execute -->
		<action name="hello" class="com.dk.action.HelloAction"
			method="execute">
			<!-- 根据HelloAction.execute返回
				的String找到result定义 -->
			<result name="success" type="dispatcher">
				/WEB-INF/hello.jsp
			</result>
		</action>
	</package>
	
</struts>

 在上面的Struts配置文件中,组织所有“用户user”和“雇员emp”配置设置在一个文件中,这不是建议的,必须回避。应该打破这种形式,而将struts.xml文件分成更小的相关模块部分。

在Struts2,应该给每个模块一个Struts配置文件。在struts.xml 设置中,包含 其他配置文件

下面通过案例演示:

演示案例 

演示工程目录结构

使用标签需要 jstl 

案例功能: 

##列表显示
需求:利用Struts2实现员工列表显示功能
/emp/list.action
-->Filter控制器-->(struts.xml)
-->EmpListAction.execute
(在execute方法中构建一个List<Emp>集合返回给JSP)
-->Result(dispatcher)
-->/WEB-INF/emp_list.jsp
(利用JSTL和EL表达式将List<Emp>生成table列表)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name>struts2-day01</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- Filter主控制器,在struts-core.jar包中 -->
	<!-- <filter> <filter-name>strutsfilter</filter-name> 默认只有.action或没有扩展名的请求 
		才能进入struts.xml,调用Action <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 
		</filter-class> </filter> <filter-mapping> <filter-name>strutsfilter</filter-name> 
		<url-pattern>/*</url-pattern> </filter-mapping> -->

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
		</init-param>

	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 	<package name="test" namespace="/emp" extends="struts-default">
		<action name="list" class="com.dk.action.EmpListAction">
			<result name="success">
				/WEB-INF/emp_list.jsp
			</result>
		</action>
	</package> -->
	<include file="struts/emp-struts.xml"></include><!-- 相对于类路径 -->
        <include file="struts/user-struts.xml"></include><!-- 相对于类路径 -->
	
	
	<!-- name唯一性标识
		extends继承框架里的Struts-default包 -->
	<package name="demo1" extends="struts-default">
		<!-- name请求名;class指定Action组件
			method指定Action方法名,默认execute -->
		<action name="hello" class="com.dk.action.HelloAction"
			method="execute">
			<!-- 根据HelloAction.execute返回
				的String找到result定义 -->
			<result name="success" type="dispatcher">
				/WEB-INF/hello.jsp
			</result>
		</action>
	</package>
	
</struts>

emp-struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name="emp" namespace="/emp" extends="struts-default">
		<action name="list" class="com.dk.action.EmpListAction">
			<result name="success">
				/WEB-INF/emp_list.jsp
			</result>
		</action>
	</package>
</struts>

 user-struts.xml 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
    <package name="demo2" namespace="/user" extends="struts-default">
		<!-- 请求toLogin会自动调用框架底层
			默认的Action的execute方法处理,返回"success" -->
		<action name="toLogin">
			<!-- name属性默认为success
				type属性默认为dispatcher -->
			<result>
				/WEB-INF/login.jsp
			</result>
		</action>
		<!-- login.action请求调用
			LoginAction的execute方法 -->
		<action name="login" class="com.dk.action.LoginAction">
			<result name="success">
				/WEB-INF/ok.jsp
			</result>
			<result name="error">
				/WEB-INF/login.jsp
			</result>
		</action>
	</package>
	
</struts>

EmpListAction.java

package com.dk.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.dk.entity.Emp;
import com.dk.entity.User;
import com.opensymphony.xwork2.ActionSupport;

public class EmpListAction extends ActionSupport{
	
	private User user;
	
	public String execute(){
		System.out.println("ACTION...");
		List<Emp> empList = new ArrayList<Emp>();
		
		for(int i=0;i<10;i++){
			Emp e = new Emp();
			e.setName("test"+i);
			e.setNo(i);
			e.setSal(1000.0);
			empList.add(e);
		}
		
		HttpServletRequest httpRequest = ServletActionContext.getRequest();
		httpRequest.setAttribute("empList", empList);
		return SUCCESS;
	}

	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}

Emp.java

package com.dk.entity;

import java.io.Serializable;

public class Emp implements Serializable{

	private Integer no;
	private String name;
	private Double sal;
	
	public Integer getNo() {
		return no;
	}
	public void setNo(Integer no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getSal() {
		return sal;
	}
	public void setSal(Double sal) {
		this.sal = sal;
	}
}
<%@page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8" import="com.dk.entity.*,java.util.*"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 <!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=ISO-8859-1">
 		<title>emp-list</title>
 	</head>
 	<body>
 		<h1>OK-${empList[0].name }</h1>
 		<table>
 			<tr>
 				<th>姓名</th>
 				<th>ID</th>
 				<th>薪资</th>
 			</tr>
 			<!-- JSTL 与  EL -->
 			<%-- <c:forEach items="${empList }" var="emp">
 				<tr>
 					<td>${emp.name }</td>
 					<td>${emp.no }</td>
 					<td>${emp.sal }</td>
 				</tr>
 			</c:forEach> --%>
 			
 			<!-- JSP的写法 -->
 			
 			<%
 				List<Emp> list = (List<Emp>)request.getAttribute("empList");
 				for(Emp e:list){
 			%>
 			<tr>
 				<td><%=e.getName() %></td>
 				<td><%=e.getNo() %></td>
 				<td><%=e.getSal() %></td>
 			</tr>
 			<%
 				}
 			%>
 		</table>
 	</body>
 </html>

加载工程 struts2-day01,启动Tomcat运行程序,最终页面效果:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_Boy_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值