一.struts.xml详解
1.package:包 属性:name=命名 namespace=在action访问路径前面加的前缀 extends=继承另外的包
action:name=action的访问路径 class="action"的完整包路径 method=指定访问的方法 (还可以url动态访问)
gboal-result:全局的跳转
intercepts:拦截器
gboal-exception-mapping: 异常拦截处理
2.include:可以将struts.xml拆分成多个配置文件,将其引用 file="文件路径"
3.constant:常量,是一些功能的开关
二.动态访问action中的自定义方法
在url后面加上!+方法名
例如:http://localhost:8888/Struts2/stu!select
三.结果类型:
<result name="xxx" type="value">stu!select?page=1</result>
1.dispatcher:转发,默认
2.redirect:重定向
3.stream:流,用于文件下载
如果一个结果不是引导到页面而是继续访问下一个action中的操作,其type必须是redirect
实例:增删改查
CRUD.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="crud!add">新增</a><br> <a href="crud!del">删除</a><br> <a href="crud!update">修改</a><br> <a href="crud!query">查询</a><br> </body> </html>
Struts.xml
<?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> <include file="struts-gy.xml"></include> </struts>
Struts-gy.xml
<?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="my" namespace="/" extends="struts-default"> <action name="login" class="com.action.LoginAction" method="login"> <result name="success" type="redirect">/jsp/success.jsp</result> </action> <action name="crud_add" class="com.action.CRUDAction" method="add"> <result name="success">/jsp/success.jsp</result> </action> <action name="crud_del" class="com.action.CRUDAction" method="del"> <result name="success">/jsp/success.jsp</result> </action> <action name="crud" class="com.action.CRUDAction"> <result name="success">/jsp/success.jsp</result> <result name="del" type="redirect">/crud!query</result> </action> </package> </struts>
CRUDAction.java
package com.action; public class CRUDAction { public String add(){ System.out.println("新增"); return "success"; } public String del(){ System.out.println("删除"); return "del"; } public String update(){ System.out.println("修改"); return "success"; } public String query(){ System.out.println("查询"); return "success"; } }