背景
昨天,第一次接触Struts2,就依葫芦画瓢,根据Struts2提供的example,写出了一个最简单的HelloStruts Demo。但是对于其实比较重要的struts.xml文件内容,其实却是一问三不知。今天将这个文件中的内容,做一个简单的总结和解释。为后面的学习先夯实一下基础。
NameSpace命名空间
1. 分析
根据以下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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 首先说说这个package,和java中的包其实是一个作用,为了分别同名类,这里是为了分别同名action -->
<package name="front" namespace="/front" extends="struts-default">
<!-- 可以看到这个xml文件有两个package,都有同名action,本package有一个namespace属性"/front" -->
<!-- 这代表,如果再url的位置打localhost:8080/Struts2_0200_NameSpace/front/hello_struts -->
<!-- 便是这个action起作用,跳转到index.jsp -->
<action name="hello_struts">
<result>/index.jsp</result>
</action>
</package>
<!-- 这个package没有namespace属性,即使用默认值"" -->
<!-- 表示只要是以/hello_struts结尾,只要找不到精确namespace的都跳转到这个result -->
<!-- 例如:localhost:8080/Struts2_0200_NameSpace/fsdafd/hello_struts -->
<package name="back" extends="struts-default">
<action name="hello_struts">
<result>/error.jsp</result>
</action>
</package>
</struts>
【附】
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>Hello Struts2</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>
Hello Struts2 <br>
</body>
</html>
error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>Error 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>
Your url is not found <br>
</body>
</html>
2. 结果
URL:localhost:8080/Struts2_0200_NameSpace/front/hello_struts时显示页面
URL:localhost:8080/Struts2_0200_NameSpace/(乱打内容)/hello_struts时显示页面
Action
大致解释一下Action里面的class是干嘛的,Action接口,等一系列的东西。前面我们用到的struts.xml文件,其实都是最简易的形式,现在把这个形式丰富一下。知道一下里面到底有什么东西。
1. 分析
直接上代码,我们代码里看各个部分都是什么意思
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>
<!-- 以下这句表示打开develop mode开发者模式,这样在struts.xml上的修改后台会实时刷新 -->
<constant name="struts.devMode" value="true" />
<!-- namespace为/等于上面提到的默认格式 -->
<package name="front" extends="struts-default" namespace="/">
<!-- action多了一个class属性,表示,这个action的操作,是由这个class决定的。 -->
<!-- 与struts1不同,每一个action都会自动new一个class相对应的对象,如果没有指定就用默认Action -->
<!-- 可以看到这里的Action是由IndexAction1决定的 -->
<action name="index" class="com.bjsxt.struts2.front.action.IndexAction1">
<!-- 根据Action中,execute方法返回的值,决定对应某个result,跳转到哪个页面 -->
<result name="success">/ActionIntroduction.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
IndexAction1.class
package com.bjsxt.struts2.front.action;
public class IndexAction1 {
public String execute() {
//这里如果返回success,则调用ActionIntroduction.jsp
return "error";
}
}
ActionIntroduction.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
具体视图的返回可以由用户自己定义的Action来决定<br />
具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容<br />
具体Action的实现可以是一个普通的java类,里面有public String execute方法即可<br />
或者实现Action接口<br />
不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法<br />
</body>
</html>
error.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 'error' 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>
ERROR! <br>
</body>
</html>
2. 结果
如果Action返回success
如果Action返回error
3. 基本框图
若有不足之处,请不吝赐教