Struts2框架之struts.xml文件配置

struts.xml文件的具体配置如下:

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
 3 <struts>
 4 <!-- 声明包 ,使用默认的命名空间-->
 5 <package name="default" extends="struts-default">
6 <!-- 定义action -->
7 8 <action name="first">

9 <!-- 定义处理成功后的映射页面 -->
10 <result name="success">
11 /first.jsp
12 </result>
13 </action>
14 
15 </package>
16 </struts>

 

 

从上面的代码很明显可以看出配置过程从外到内层的属性配置大致为:配置包->Action定义->定义映射关系

  1.  包配置

    name:必填,指定包名

    extends: 可选指定该包继承其他包,为了实现Struts的核心功能 (拦截器的拦截功能)包通常都应该继承struts-default包

    namespace: 可选,用于指定命名空间 ,当不对该属性进行指定的时候默认使用命名空间为“”。

  2. Action定义

     <action name="first">此处除了可定义处理请求的URL,还可以写入其相对应处理请求的Action(java类)

            即可写成:<action name="first" class=“action.LoginAction">(action包下的LoginAction类继承了ActionSupport)

  3. 定义映射关系
    <result name="success">/first.jsp</result>根据Action的返回值导向跳转到指定的URL,此处
    success为Action返回值;
    /first.jsp为指定的页面。




相关文件:
index.jsp
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24   <a href="first.action">请求Struts2</a>
25  <br>
26   </body>
27 </html>

    first.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 'first.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>
HuangDong
    <br>
  </body>
</html>

2016-05-04 11:31:21

 

你可能感兴趣的:(Struts2框架之struts.xml文件配置)