struts2-bymailreader案例练习(一)
前言:struts2-bymailreader是在学习了官方struts-mailreader后的简单练习。官方struts-mailreader案例中有一个专属的struts-mailreader-dao-1.3.5.jar包,如要更好的理解案例,最好将该包反编译查看其源码。
一 创建项目
1,导入jar包,在web.xml配置过滤器
<?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>struts2-bymailreader</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <filter> <filter-name>mymailreader</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>mymailreader</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> </web-app>
其中,jar包在官方案例中获取,过滤器class在struts2-core-2.3.34.jar的XX-ng-filter目录下拷贝全路径得到。尝试使用只让.do后缀的请求进入过滤器。
2,创建index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" http-equiv="refresh" content="1;URL=Welcome.do"> <title>Insert title here</title> </head> <body> <h1>index refrese to Welcome ..</h1> </body> </html>
3,在struts.xml中配置匹配Welcome.do的action
<?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="do"></constant> <!-- 定义全局action的拓展后缀为do --> <constant name="struts.devMode" value="false" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <package name="default" namespace="/" extends="struts-default"> <action name="Welcome" class="bymailreader.Welcome"> <result>Welcome.jsp</result> </action> </package> </struts>
默认只匹配后缀为.aciton的请求,设置struts.action.extension,让action能匹配.do的请求。
4,创建Welcome.jsp
<%@ 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>Insert title here</title> </head> <body> <h1>Welcome..</h1> </body> </html>
5,创建名为Welcome的action的实现类Welcome.java,位于bymailreader包内。
package bymailreader; import com.opensymphony.xwork2.ActionSupport; public class Welcome extends ActionSupport{ @Override public String execute() throws Exception { //1,判断是否配置了定义了datebase不存在的描述 String message = getText(Constants.ERROR_DATABASE_MISSING); if(Constants.ERROR_DATABASE_MISSING.equals(message)){ this.addActionError(Constants.ERROR_DATEBASE_NOT_LOADED); } //2,处理返回结果 if(this.hasErrors()){ return ERROR; }else{ return SUCCESS; } } }
类Welcome继承了ActionSupport类,因此可以使用ActionSupport类的addActionError()和hasErrors()方法。
6,在包内创建Constants.java常量定义类和创建package.properties配置文件
package bymailreader; /** * 常量类 */ public final class Constants { //datebase未配置时的参数 public static final String ERROR_DATABASE_MISSING = "error.database.missing"; }
error.database.missing = 无法连接到数据库
7,在struts.xml中配置全局ERROR的result处理
<package name="default" namespace="/" extends="struts-default"> <global-results> <result name="error">error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Throwable"></exception-mapping> </global-exception-mappings> <!-- 定义全局error返回处理 --> <action name="Welcome" class="bymailreader.Welcome"> <result>Welcome.jsp</result> </action> </package>
8,创建errror.jsp
<%@ 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>Insert title here</title> </head> <body> <h1>ERROR ..</h1> </body> </html>
二:运行项目 http://localhost:8080/struts2-bymailreader/
将package.properties中配置注释掉,得到如下运行结果
参考资料:https://blog.csdn.net/winder9898/article/details/52190613