1. 程式人生 > >第11講 struts2的struts.xml的萬用字元控制

第11講 struts2的struts.xml的萬用字元控制

1複製HeadFirstStruts2chapter02-04,改名:HeadFirstStruts2chapter02-05,修改web project settings匯入StudentAction和GradeAction,刪除其他多餘的Action,
StudentAction如下:

package com.cruise.action;

import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport{

    private

 String name;
    
    public String getName() {
       return name;
    }

    public void setName(String name) {
       this
.name = name;
    }

    public String list()throws Exception{
       System.out.println("學生查詢");
       name="學生查詢";
       return SUCCESS;
    }
    
    public
 String add()throws Exception{
       System.out.println("學生新增");
       name="學生新增";
       return SUCCESS;
    }
    
    public String update()throws Exception{
       System.out.println("學生修改");
       name="學生修改";
       return SUCCESS;
    }
    
    public String delete()throws Exception{
       System.out.println("學生刪除");
       name="學生刪除";
       return SUCCESS;
    }
}
GradeAction如下:
package com.cruise.action;

import com.opensymphony.xwork2.ActionSupport;

public class GradeAction extends ActionSupport{

    private String name;
    
    
    public String getName() {
       return name;
    }

    public void setName(String name) {
       this.name = name;
    }
    public String list()throws Exception{
       System.out.println("班級查詢");
       name="班級查詢";
       return SUCCESS;
    }
    
    public String add()throws Exception{
       System.out.println("班級新增");
       name="班級新增";
       return SUCCESS;
    }
    
    public String update()throws Exception{
       System.out.println("班級修改");
       name="班級修改";
       return SUCCESS;
    }
    
    public String delete()throws Exception{
       System.out.println("班級刪除");
       name="班級刪除";
       return SUCCESS;
    }
}
2刪除多餘.xml檔案,保留struts.xml檔案,修改struts.xml檔案,使用佔位符來確定方法名,
xml version="1.0" encoding="UTF-8" ?>
DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="manage" namespace="/" extends="struts-default">
    <action name="student_*" class="com.cruise.action.StudentAction" method="{1}">
       <result name="success">success.jspresult>
    action>
    
    <action name="grade_*" class="com.cruise.action.GradeAction" method="{1}">
       <result name="success">success.jspresult>
    action>
package>
struts>
ps:如果標籤的namespace不是“/”,則在標籤裡面跳轉頁面路徑的時候,需要在前面加${pageContext.request.contextPath}/
3匯入index.jsp 檔案,修改success.jsp檔案,
index.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 heretitle>
head>
<body>
<h1>萬用字元h1>
<a href="student_list" target="_blank">學生資訊查詢a> 
<a href="student_add" target="_blank">學生資訊新增a> 
<a href="student_update" target="_blank">學生資訊修改a> 
<a href="student_delete" target="_blank">學生資訊刪除a> 
<br/><br/>
<a href="grade_list" target="_blank">班級資訊查詢a> 
<a href="grade_add" target="_blank">班級資訊新增a> 
<a href="grade_update" target="_blank">班級資訊修改a> 
<a href="grade_delete" target="_blank">班級資訊刪除a> <br/>
body>
html>

success.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 heretitle>
head>
<body>
OK!!${name}
body>
html>
4測試
http://localhost:8080/HeadFirstStruts2chapter02_05
5萬用字元升級版:修改struts.xml檔案。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="manage" namespace="/" extends="struts-default">
    <action name="*_*" class="com.cruise.action.{1}Action" method="{2}">
       <result name="success">success.jsp</result>
    </action>
</package>
</struts>

6萬用字元升級版:修改index.jsp,修改首字母的大小寫,這裡主要是和Action匹配
<%@ 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>萬用字元</h1>
<a href="Student_list" target="_blank">學生資訊查詢</a>&nbsp;
<a href="Student_add" target="_blank">學生資訊新增</a>&nbsp;
<a href="Student_update" target="_blank">學生資訊修改</a>&nbsp;
<a href="Student_delete" target="_blank">學生資訊刪除</a>&nbsp;
<br/><br/>
<a href="Grade_list" target="_blank">班級資訊查詢</a>&nbsp;
<a href="Grade_add" target="_blank">班級資訊新增</a>&nbsp;
<a href="Grade_update" target="_blank">班級資訊修改</a>&nbsp;
<a href="Grade_delete" target="_blank">班級資訊刪除</a>&nbsp;<br/>
</body>
</html>
7測試