标签:默认 返回值 jdk mon 管理 使用 字符串 mic 目的
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping><?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> <package name="default" namespace="/" extends="struts-default"> <action name="hello" class="cn.itcast.action.HelloAction" method="say"> <result name="good">/hello.jsp</result> </action> </package> <package name="dd" namespace="/abc" extends="struts-default"> <action name="hello" class="cn.itcast.action.HelloAction" method="say"> <result name="good">/hello.jsp</result> </action> </package> </struts>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>mystruts2_day01</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>HelloAction类
package cn.itcast.action;
public class HelloAction {
public String say() {
System.out.println("hello action say hello");
return "good";
}
}
index.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/hello">第一次使用struts2</a>
</body>
</html>
hello.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP ‘index.jsp‘ starting page</title> </head> <body> <h1>hello Struts2</h1> </body> </html>
package cn.itcast.action;
public class HelloAction {
public String say() {
System.out.println("hello action say method");
return "good";
}
}
StrutsFilterpackage cn.itcast.filter;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class StrutsFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
// 1.强转
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// 2.操作
// 2.1 得到请求资源路径
String uri = request.getRequestURI();
String contextPath = request.getContextPath();
String path = uri.substring(contextPath.length() + 1);
// System.out.println(path); // hello
// 2.2 使用path去struts.xml文件中查找某一个<action name=path>这个标签
SAXReader reader = new SAXReader();
try {
// 得到struts.xml文件的document对象。
Document document = reader.read(new File(this.getClass()
.getResource("/struts.xml").getPath()));
Element actionElement = (Element) document
.selectSingleNode("//action[@name=‘" + path + "‘]"); // 查找<action
// name=‘hello‘>这样的标签
if (actionElement != null) {
// 得到<action>标签上的class属性以及method属性
String className = actionElement.attributeValue("class"); // 得到了action类的名称
String methodName = actionElement.attributeValue("method");// 得到action类中的方法名称。
// 2.3通过反射,得到Class对象,得到Method对象
Class actionClass = Class.forName(className);
Method method = actionClass.getDeclaredMethod(methodName);
// 2.4 让method执行.
String returnValue = (String) method.invoke(actionClass
.newInstance()); // 是让action类中的方法执行,并获取方法的返回值。
// 2.5
// 使用returnValue去action下查找其子元素result的name属性值,与returnValue做对比。
Element resultElement = actionElement.element("result");
String nameValue = resultElement.attributeValue("name");
if (returnValue.equals(nameValue)) {
// 2.6得到了要跳转的路径。
String skipPath = resultElement.getText();
// System.out.println(skipPath);
request.getRequestDispatcher(skipPath).forward(request,
response);
return;
}
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 3.放行
chain.doFilter(request, response);
}
public void destroy() {
}
}
src下struts.xml<?xml version="1.0" encoding="UTF-8" ?> <struts> <action name="hello" class="cn.itcast.action.HelloAction" method="say"> <result name="good">/hello.jsp</result> </action> </struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>struts</filter-name>
<filter-class>cn.itcast.filter.StrutsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app><result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> <result-types>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>struts.action.extension</param-name> <param-value>do,,</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?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.action.extension" value="def,,"></constant> <constant name="struts.devMode" value="false"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <include file="default.xml"></include> <include file="book.xml"></include> <include file="servlet.xml"></include> </struts>
struts.action.extension=abc,,BookAction
package cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport {
public String add(){
System.out.println("book add");
return NONE;
}
public String update(){
System.out.println("book update");
return NONE;
}
public String delete(){
System.out.println("book delete");
return NONE;
}
public String search(){
System.out.println("book search");
return NONE;
}
}
package cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class DefaultAction extends ActionSupport {
@Override
public String execute() throws Exception {
// return null; 或return NONE; 会跳转一空白页面
System.out.println("DefaultAction execute Method......");
return SUCCESS;
}
}
package cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class DefaultAction2 extends ActionSupport {
@Override
public String execute() throws Exception {
// return null; 或return NONE; 会跳转一空白页面
System.out.println("DefaultAction2 execute Method......");
return SUCCESS;
}
}
package cn.itcast.action;
public class HelloAction {
public String say() {
System.out.println("hello action say method");
return "good";
}
public String write() {
System.out.println("hello action write method");
return "hehe";
}
}
ProductActionpackage cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class ProductAction extends ActionSupport {
public String add(){
System.out.println("product add");
return NONE;
}
public String update(){
System.out.println("product update");
return NONE;
}
public String delete(){
System.out.println("product delete");
return NONE;
}
public String search(){
System.out.println("product search");
return NONE;
}
}
package cn.itcast.action;
import java.util.Map;
import cn.itcast.utils.PrintMap;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
//获取Servlet API 通过ActionContext获取
public class ServletDemo1Action extends ActionSupport {
public String execute() throws Exception {
// 1.获取ActionContext
ActionContext context = ActionContext.getContext();
// 2.获取Servlet API
// 2.1获取application中的数据
Map<String, Object> applicationMap = context.getApplication();
PrintMap.print(applicationMap);
// 2.2获取session中的数据
Map<String, Object> sessionMap = context.getSession();
PrintMap.print(sessionMap);
// 2.3获取请求参数
Map<String, Object> paramMap = context.getParameters();
PrintMap.print(paramMap);
// 2.4 向request范围存储数据
context.put("username", "tom");
return SUCCESS;
}
}
ServletDemo2Actionpackage cn.itcast.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
// 获取Servlet api 通过注入方式
public class ServletDemo2Action extends ActionSupport implements
ServletRequestAware {
private HttpServletRequest request;
@Override
public String execute() throws Exception {
System.out.println(request.getParameter("username"));
return null;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
}
ServletDemo3Actionpackage cn.itcast.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
// 获取Servlet api 通过ServletActionContext获取
public class ServletDemo3Action extends ActionSupport {
@Override
public String execute() throws Exception {
HttpServletRequest request=ServletActionContext.getRequest();
System.out.println(request.getParameter("username"));
return null;
}
}
PrintMappackage cn.itcast.utils;
import java.util.Map;
public class PrintMap {
public static void print(Map<String, Object> map) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + "——" + entry.getValue());
}
System.out.println("————————————————————————");
}
}
<?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>
<package name="bookPackage" namespace="/b" extends="struts-default">
<!--
<action name="Book_add" class="cn.itcast.action.BookAction" method="add"></action>
<action name="Book_update" class="cn.itcast.action.BookAction" method="update"></action>
<action name="Book_delete" class="cn.itcast.action.BookAction" method="delete"></action>
<action name="Book_search" class="cn.itcast.action.BookAction" method="search"></action>
-->
<action name="*_*" class="cn.itcast.action.{1}Action" method="{2}"></action>
</package>
</struts>
<?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>
<!-- 自定义一个POJO类 -->
<package name="pojoPackage" namespace="/" extends="struts-default">
<!-- 当前包下的默认Action 即当以namespace="/"开头的路径找不到时, 就会跳转 /hello 注意:只包含一级目录 例如:/www找不到时会跳转
如果/www/xx/aaa找不到时就会报错 -->
<default-action-ref name="hello1"></default-action-ref>
<action name="hello" class="cn.itcast.action.HelloAction"
method="say">
<result name="good">/hello.jsp</result>
</action>
<action name="hello1" class="cn.itcast.action.HelloAction"
method="write">
<result name="hehe">/hehe.jsp</result>
</action>
</package>
<package name="defaultPackage" namespace="/a" extends="struts-default">
<default-action-ref name="default"></default-action-ref>
<!-- 当前包下的Action默认处理类 如果不声明,则默认执行 com.opensymphony.xwork2.ActionSupport类 -->
<default-class-ref class="cn.itcast.action.DefaultAction2"></default-class-ref>
<!-- 继承自ActionSupport类 使用默认值 -->
<action name="default" class="cn.itcast.action.DefaultAction">
<result>/default.jsp</result>
</action>
<action name="good">
<result>/good.jsp</result>
</action>
</package>
</struts>
<?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> <package name="default" namespace="/" extends="struts-default"> <!-- 全局结果页面 --> <global-results> <result>/demo1_success.jsp</result> </global-results> <action name="demo1" class="cn.itcast.action.ServletDemo1Action"> <!-- 局部结果页面 --> </action> <action name="demo2" class="cn.itcast.action.ServletDemo2Action"> <!-- <result>/demo1_success.jsp</result> --> </action> <action name="demo3" class="cn.itcast.action.ServletDemo3Action"> <!-- <result type="redirect">/demo1_success.jsp</result> --> </action> </package> </struts>book.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/Book_add">book add</a><br>
<a href="${pageContext.request.contextPath}/Book_update">book update</a><br>
<a href="${pageContext.request.contextPath}/Book_delete">book delete</a><br>
<a href="${pageContext.request.contextPath}/Book_search">book search</a><br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<h1>hello default</h1>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<%
session.setAttribute("sname", "svalue");
application.setAttribute("aname", "avalue");
%>
<a href="${pageContext.request.contextPath}/demo1">访问demo1servletAction</a>
</body>
</html>
demo1_success.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
${username}
</body>
</html>
good.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<h1>hello GOOD</h1>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<h1>hello 呵呵</h1>
</body>
</html>
hello.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<h1>hello Struts2</h1>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/hello">第一次使用struts2</a>
<br>
<a href="${pageContext.request.contextPath}/a/default">使用struts2继承默认ActionSupport</a>
<br>
</body>
</html>
product.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/Product_add">product add</a><br>
<a href="${pageContext.request.contextPath}/Product_update">product update</a><br>
<a href="${pageContext.request.contextPath}/Product_delete">product delete</a><br>
<a href="${pageContext.request.contextPath}/Product_search">product search</a><br>
</body>
</html>
JAVAWEB开发之Struts2详解(一)——Struts2框架介绍与快速入门、流程分析与工具配置以及Struts2的配置以及Action和Result的详细使用
标签:默认 返回值 jdk mon 管理 使用 字符串 mic 目的
原文地址:http://blog.csdn.net/u013087513/article/details/60578692