struts2中Convention plug-in中的那些事儿
?
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2Example1</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>
?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib uri="/struts-tags" prefix="s" %><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title> </head> <body> <s:form action="welcome-user" > <s:textfield name="userName" label="User Name" /> <s:submit /> </s:form> </body></html>?
package com.vaannila.action;import com.opensymphony.xwork2.ActionSupport;public class WelcomeUser extends ActionSupport{private String userName;private String message;public String execute() {message = "Welcome " + userName;return SUCCESS;}public void setUserName(String userName) {this.userName = userName;}public void setMessage(String message) {this.message = message;}public String getUserName() {return userName;}public String getMessage() {return message;}}
??<!--[endif]-->下面的是用来显示结果的页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!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=ISO-8859-1"><title>Welcome User</title></head><body><h1>${message}</h1></body></html>
?至此已经完成了。。。。
/** * Makes all the results for the given path. * * @param actionClass The action class the results are being built for. * @param path The path to build the result for. * @param resultPrefix The is the result prefix which is the result location plus the action name. * This is used to determine if the path contains a result code or not. * @param results The Map to place the result(s) * @param packageConfig The package config the results belong to. * @param resultsByExtension The map of extensions to result type configuration instances. */ protected void makeResults(Class<?> actionClass, String path, String resultPrefix, Map<String, ResultConfig> results, PackageConfig packageConfig, Map<String, ResultTypeConfig> resultsByExtension) { if (path.startsWith(resultPrefix)) { int indexOfDot = path.indexOf('.', resultPrefix.length()); // This case is when the path doesn't contain a result code if (indexOfDot == resultPrefix.length() || !flatResultLayout) { if (LOG.isTraceEnabled()) { LOG.trace("The result file [#0] has no result code and therefore" + " will be associated with success, input and error by default. This might" + " be overridden by another result file or an annotation.", path); }//下面就是分析是否包含有返回结果类型的页面了 if (!results.containsKey(Action.SUCCESS)) { ResultConfig success = createResultConfig(actionClass, new ResultInfo(Action.SUCCESS, path, packageConfig, resultsByExtension), packageConfig, null); results.put(Action.SUCCESS, success); } if (!results.containsKey(Action.INPUT)) { ResultConfig input = createResultConfig(actionClass, new ResultInfo(Action.INPUT, path, packageConfig, resultsByExtension), packageConfig, null); results.put(Action.INPUT, input); } if (!results.containsKey(Action.ERROR)) { ResultConfig error = createResultConfig(actionClass, new ResultInfo(Action.ERROR, path, packageConfig, resultsByExtension), packageConfig, null); results.put(Action.ERROR, error); } // This case is when the path contains a result code } else if (indexOfDot > resultPrefix.length()) { if (LOG.isTraceEnabled()) { LOG.trace("The result file [#0] has a result code and therefore" + " will be associated with only that result code.", path); } String resultCode = path.substring(resultPrefix.length() + 1, indexOfDot); ResultConfig result = createResultConfig(actionClass, new ResultInfo(resultCode, path, packageConfig, resultsByExtension), packageConfig, null); results.put(resultCode, result); } } }
?..................................