标签:动态 method struts set message ali tty extends mic
一、简述
MVC全名Model View Controller,是一种将业务逻辑、数据和界面显示分离的方法。
Struts是一个基于MVC模式设计的Web应用框架,其各层结构功能如下:
Struts 2的请求执行流程如下:
二、控制器
Action是Struts 2框架的核心。每个URL映射到特定的Action上,后者提供对用户的请求进行处理所需的逻辑。
Action的两个重要功能分别是将数据从请求传递到视图和协助框架确定什么结果应该呈现在视图中。
Action提供了部分处理结果的预定义字符串变量:
静态变量 | 说明 |
ERROR | Action执行失败的返回值,如验证信息错误。 |
INPUT | 返回某个输入信息页面的返回值,如修改信息页面。 |
LOGIN | 需要用户登陆的返回值,如登陆验证失败需要重新登陆。 |
NONE | Action执行成功的返回值,不返回到成功页面。 |
SUCCESS | Action执行成功的返回值,返回到成功页面。 |
ActionSupport类实现了Action接口,创建控制器时一般需要继承该类。
用户提交表单后信息会自动注入到与Action对象响应的属性中,前提是在Action类中必须提供属性的setter方法。
//MessageAction.java package action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import java.util.Map; public class MessageAction extends ActionSupport { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public String execute() throws Exception { Map<String, Object> session = ActionContext.getContext().getSession(); session.put("message", message); return (message == null || message.isEmpty()) ? ActionSupport.ERROR : ActionSupport.SUCCESS; } } //index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <form action="MessageAction.action" method="post"> <input type="text" name="message" id="message" placeholder="输入文本"/> <input type="submit" value="提交"/> </form> <c:if test="${sessionScope.message != null && !sessionScope.message.isEmpty()}"> <c:out value="文本:${sessionScope.message}"/> </c:if> </body> </html> //struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- namespace决定了Action的访问路径 --> <package name="action" namespace="/" extends="struts-default"> <action name="MessageAction" class="action.MessageAction"> <result name="error">/index.jsp</result> <result name="success">/index.jsp</result> </action> </package> </struts> //web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <filter> <filter-name>struts2</filter-name> <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>
三、动态方法调用
动态方法调用是为了让Action对应多个请求处理,以避免Action的数量太多。
1.感叹号方式是在请求Action的URL地址后加上请求字符串(方法名),与Action对象中的方法进行匹配。URL与请求字符串之间以感叹号进行分隔,该方式一般不推荐使用。
//index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <a href="OperateAction!insert.action">添加</a> <a href="OperateAction!delete.action">删除</a> <a href="OperateAction!update.action">更新</a> <a href="OperateAction!select.action">查询</a> <c:if test="${sessionScope.operate ne null}"> <c:out value="操作:${sessionScope.operate}"/> </c:if> </body> </html> //OperateAction.java package action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import java.util.Map; public class OperateAction extends ActionSupport { private String setAttribute(String operate) { Map<String, Object> session = ActionContext.getContext().getSession(); session.put("operate", operate); return operate; } public String insert() { return setAttribute("insert"); } public String delete() { return setAttribute("delete"); } public String update() { return setAttribute("update"); } public String select() { return setAttribute("select"); } } //struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- Struts 2.5后调用动态方法需要关闭strict-method-invocation --> <package name="action" namespace="/" extends="struts-default" strict-method-invocation="false"> <action name="OperateAction" class="action.OperateAction"> <result name="select">/index.jsp</result> <result name="delete">/index.jsp</result> <result name="update">/index.jsp</result> <result name="insert">/index.jsp</result> </action> </package> </struts>
2.用户发送Action请求时,在默认情况下执行execute方法。但在多个业务逻辑分支的Action对象中,需要通过指定<action>标签中的method属性,从而调用对应的逻辑方法进行处理。
//OperateAction.java package action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import java.util.Map; public class OperateAction extends ActionSupport { private void setAttribute(String operate) { Map<String, Object> session = ActionContext.getContext().getSession(); session.put("operate", operate); } public String insert() { setAttribute("insert"); return ActionSupport.SUCCESS; } public String delete() { setAttribute("delete"); return ActionSupport.SUCCESS; } public String update() { setAttribute("update"); return ActionSupport.SUCCESS; } public String select() { setAttribute("select"); return ActionSupport.SUCCESS; } } //index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <a href="insertAction.action">添加</a> <a href="deleteAction.action">删除</a> <a href="updateAction.action">更新</a> <a href="selectAction.action">查询</a> <c:if test="${sessionScope.operate ne null}"> <c:out value="操作:${sessionScope.operate}"/> </c:if> </body> </html> //struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="action" namespace="/" extends="struts-default" strict-method-invocation="false"> <action name="insertAction" class="action.OperateAction" method="insert"> <result name="success">/index.jsp</result> </action> <action name="deleteAction" class="action.OperateAction" method="delete"> <result name="success">/index.jsp</result> </action> <action name="updateAction" class="action.OperateAction" method="update"> <result name="success">/index.jsp</result> </action> <action name="selectAction" class="action.OperateAction" method="select"> <result name="success">/index.jsp</result> </action> </package> </struts>
3.在配置文件struts.xml中支持使用通配符,达到简化配置代码的效果。是动态方法调用中最常用的一种方式。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="action" namespace="/" extends="struts-default" strict-method-invocation="false"> <action name="*Action" class="action.OperateAction" method="{1}"> <result name="success">/index.jsp</result> </action> </package> </struts>
四、映射类型
在Web项目配置Struts 2框架,使用Action对象处理用户的请求。这时一般通过ActionContext对象获取映射类型的request、session和application变量,用于保存处理后的信息。
//SetAction.java package action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import java.util.Map; public class SetAction extends ActionSupport { @Override @SuppressWarnings("unchecked") public String execute() throws Exception { ActionContext context = ActionContext.getContext(); Map<String, Object> request = (Map<String, Object>) context.get("request"); Map<String, Object> session = context.getSession(); Map<String, Object> application = context.getApplication(); request.put("requestValue", 0); session.put("sessionValue", 1); application.put("applicationValue", 2); return ActionSupport.SUCCESS; } } //struts.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="action" namespace="/" extends="struts-default"> <action name="setAction" class="action.SetAction"> <result>/index.jsp</result> </action> </package> </struts> //index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <a href="setAction.action">获取</a> <c:if test="${requestScope.requestValue ne null}"> <c:out value="Request Value = ${requestScope.requestValue}"/> </c:if> <c:if test="${sessionScope.sessionValue ne null}"> <c:out value="Session Value = ${sessionScope.sessionValue}"/> </c:if> <c:if test="${applicationScope.applicationValue ne null}"> <c:out value="Application Value = ${applicationScope.applicationValue}"/> </c:if> </body> </html>
标签:动态 method struts set message ali tty extends mic
原文地址:https://www.cnblogs.com/arseneyao/p/9405837.html