struts模式匹配

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

	<action-mappings>
		<!-- 
		<action path="/user/user_mgr"
				type="com.bjpowernode.struts.UserAction"
				parameter="command"
		>
			<forward name="add_success" path="/user/add_success.jsp"/>
			<forward name="del_success" path="/user/del_success.jsp"/>
			<forward name="update_success" path="/user/update_success.jsp"/>
		</action>
		 -->
		 
		<action path="/user/*"
				type="com.bjpowernode.struts.UserAction"
				parameter="command"
		>
		<!-- 
			<forward name="success" path="/user/{1}_success.jsp"/>
		 -->	
		 <forward name="success" path="/{0}_success.jsp"/>
		</action>
		 
	</action-mappings>
</struts-config>



<forward name="success" path="/{0}_success.jsp"/>中的{0}表示取得user/command, command为del、add、update

package com.bjpowernode.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class UserAction extends DispatchAction {

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//return mapping.findForward("add_success");
		return mapping.findForward("success");
	}

	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//return mapping.findForward("del_success");
		return mapping.findForward("success");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//return mapping.findForward("update_success");
		return mapping.findForward("success");
	}
	
}



<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
  	<!-- 
	<a href="user/user_mgr.do?command=add">添加</a><br>    
	<a href="user/user_mgr.do?command=del">删除</a><br>
	<a href="user/user_mgr.do?command=update">修改</a><br>
	 -->
	 
	<a href="user/add.do?command=add">添加</a><br>    
	<a href="user/del.do?command=del">删除</a><br>
	<a href="user/update.do?command=update">修改</a><br>
  </body>
</html>


del_success.jsp和update_success.jsp和这个一样
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
add_success.jsp
</body>
</html>

你可能感兴趣的:(apache,html,jsp,struts,servlet)