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>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
<include file="example.xml"/>
-->
<!-- Add packages here -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="wildcard" namespace="/" extends="struts-default">
<action name="student*" class="struts.zyl.ActionStudent" method="{1}">
<result name="success">
/Student{1}_SUCCESS.jsp
</result>
</action>
<action name="*_*" class="struts.zyl.Action{1}" method="{2}">
<result name="success">
/{1}_{2}_SUCCESS.jsp
</result>
</action>
</package>
</struts>
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
使用通配符,将配置量降到最低<fieldset><legend>学生</legend><a href="studentadd">添加学生</a><br><a href="studentdelete">删除学生</a><br></fieldset>不过,一定遵守"约定优于配置"的原则<fieldset><legend>老师</legend>
<a href="Teacher_add">添加老师</a><br><a href="Teacher_delete">删除老师</a></fieldset>
</body>
</html>
ActionStudent:
package struts.zyl;
import com.opensymphony.xwork2.ActionSupport;
public class ActionStudent extends ActionSupport{
public String add()
{
return SUCCESS;
}
public String delete()
{
return SUCCESS;
}
}
ActionTeacher:
package struts.zyl;
import com.opensymphony.xwork2.ActionSupport;
public class ActionTeacher extends ActionSupport{
public String add()
{
return SUCCESS;
}
public String delete()
{
return SUCCESS;
}
}
四个JSP:Studentadd_SUCCESS.jsp,Studentdelete_SUCCESS.jsp
Teacher_add_SUCCESS.jsp,Teacher_delete_SUCCESS.jsp
图片:
今天的文章Struts2通配符配置分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26537.html