Struts2入门
Posted 星辰大海
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2入门相关的知识,希望对你有一定的参考价值。
结构:
HelloWorldAction.java
package com.struts2.test; public class HelloWorldAction { private String name; public String execute() throws Exception { return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
web.xml提供任何Web应用程序的入口点:
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>//不写,默认为index.jsp
</welcome-file-list>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <!-- Struts2配置 --> <filter> <filter-name>struts2</filter-name> <!-- Filter的实现类 和struts2.5以前的可能有所不同 --> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>Hello World</title> </head> <body> <h1>Hello World From Struts2</h1> <form action="hello"> <label for="name">Please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
helloWorld.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!-- taglib指令告诉Servlet容器中,这个页面将使用Struts2标签,这些标签之前, 将通过s:property标签中显示的动作类的属性名称>HelloWorldAction类的getName()方法返回的值。 --> <!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>Hello World</title> </head> <body> Hello World, <s:property value="name"/> </body> </html>
struts.xml:
<?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="struts2" namespace="/" extends="struts-default"> <!-- <action name="index"> <result >/index.jsp</result> </action> --> //如果不带参数的方法不指定,则默认行为是使用execute()方法 <action name="hello" class="com.struts2.test.HelloWorldAction"> <result>/helloWorld.jsp</result> </action> <-- more actions can be listed here --> </package> <-- more packages can be listed here --> </struts>
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> <include file="struts1.xml"/> <include file="struts2.xml"/> <include file="struts3.xml"/> </struts>
logging.properties:
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = \\java.util.logging.ConsoleHandler
浏览器访问http://localhost:8080/Struts2/
点击say hello 跳转:
-------------------------------完毕---------------------------------------
结构:
accessDenied.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>Access Denied</title>
</head>
<body>
<h1>您没有权限查看此页</h1>
</body>
</html>
HelloWorldAction.java也可以extends ActionSupport
package com.struts2.test; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String name; public String execute() throws Exception { if ("成功".equals(name)) { return SUCCESS; }else{ return ERROR; } } public String getName() { return name; } public void setName(String name) { this.name = name; } }
我们可以使用字符串常量SUCCESS、ERROR
struts.xml:如果不带参数的方法不指定,则默认行为是使用execute()方法
<?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.devMode" value="true" /> <package name="helloworld" namespace="/" extends="struts-default"> <action name="hello" class="com.struts2.test.HelloWorldAction" method="execute"> <result name="success">/helloWorld.jsp</result> <result name="error">/accessDenied.jsp</result> </action> </package> </struts>
http://localhost:8080/Struts2/index.jsp
点击say hello:
点击say hello:
------------------------完毕----------------------------------------
结构:
拦截器
AbstractInterceptor类可以扩展。这提供了一个默认的无操作实现 init() 和destroy()方法。
MyInterceptor.java:
package com.struts2.test; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor { /** * */ private static final long serialVersionUID = 1L; public String intercept(ActionInvocation invocation)throws Exception{ //预处理 System.out.println("预处理"); //调用动作或下一个拦截器 String result = invocation.invoke(); //后期处理 System.out.println("后期处理"); return result; } }
HelloWorldAction.java:
package com.struts2.test; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String name; public String execute() throws Exception { System.out.println("进入HelloWorldAction...."); if ("成功".equals(name)) { return SUCCESS; }else{ return ERROR; } } public String getName() { return name; } public void setName(String name) { this.name = name; } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Struts2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- Struts2配置 --> <filter> <filter-name>struts2</filter-name> <!-- Filter的实现类 和struts2.5以前的不同 --> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
http://localhost:8080/Struts2/index.jsp
点击say hello:
控制台:
----------------------完毕-------------------------------------
结构:
FreeMaker视图技术,用于生成输出。不依赖于使用JSP作为视图技术。
hello.fm:
hello ${name} !
struts.xml:
<?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.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.struts2.test.HelloWorldAction" method="execute"> <result name="success" type="freemarker"> <param name="location">/hello.fm</param> </result> </action> </package> </struts>
http://localhost:8080/Struts2/index.jsp
点击say hello
----------------------------------完毕-------------------------------
分发调度RequestDispatcher.forward()
struts.xml
<?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.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.struts2.test.HelloWorldAction" method="execute"> <result name="success" type="dispatcher"> <param name="location"> /helloWorld.jsp </param> </result> </action> </package> </struts>
----------------------------------完毕-------------------------------
重定向 response.sendRedirect()
struts.xml:
<?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.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.struts2.test.HelloWorldAction" method="execute"> <result name="success" type="redirect"> <param name="location"> /accessDenied.jsp </param> </result> </action> </package> </struts>
----------------------------------完毕-------------------------------
OGNL对象图导航语言 用于引用和操作数据的值栈、数据传输和类型转换。
值栈/OGNL:
OGNL根据上下文和Struts建立一个ActionContext地图:
-
application - 应用程序范围内的变量
-
session - 会话范围的变量
-
root / value stack -所有操作变量都存储在这里
-
request - 请求范围的变量
-
parameters - 请求参数
-
atributes - 存储的属性页面,请求,会话和应用范围
值栈中的对象始终可用,值栈中的对象可以直接引用.
创建动作:访问值栈
HelloWorldAction.java:
package com.struts2.test; import java.util.*; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private String name; public String execute() throws Exception { ValueStack stack = ActionContext.getContext().getValueStack(); Map<String, Object> context = new HashMap<String, Object>(); context.put("key1", new String("值1")); context.put("key2", new String("值2")); stack.push(context); System.out.println("Size of the valueStack: " + stack.size()); return "success"; } //使用<s:property>标签访问值 public String getName() { return name; } public void setName(String name) { this.name = name; } }
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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>Hello World</title> </head> <body> <h1>Hello World From Struts2</h1> <form action="hello"> <label for="name">请输入您的姓名</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
struts.xml:
<?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.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.struts2.test.HelloWorldAction" method="execute"> <result name="success">/helloWorld.jsp</result> </action> </package> </struts>
helloWorld.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"《Java从入门到放弃》入门篇:Struts2的基本数据传递方式《Java从入门到放弃》入门篇:Struts2的常用验证方式