JakartaEE Struts2使用
1. Struts2下载
解压后的目录结构如下:
从一个高水平角度看,Struts2 是一个MVC拉动的(或MVC2)框架,Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的:
- 操作(Actions)
- 拦截器(Interceptors)
- 值栈(Value Stack)/OGNL
- 结果(Result)/结果类型
- 视图技术
请求生命周期
通过上述图片的描述,我们可以依照下面几点解释在Struts2 中用户的请求生命周期:
- 用户发送一个资源需求的请求到服务器(例如:页面)。
- 核心控制器查看请求后确定适当的动作。
- 使用验证、文件上传等配置拦截器功能。
- 执行选择的动作来完成请求的操作。
- 如果需要的话,配置的拦截器可做任何后期处理。
- 最后,由视图显示结果并返回给用户。
2. HelloWorld程序
1. 四个组件:
- Action(操作)
创建一个动作类,包含完整的业务逻辑并控制用户、模型以及视图间的交互。 - Interceptors(拦截器)
这是控制器的一部分,可依据需求创建拦截器,或使用现有的拦截器。 - View(视图)
创建一个JSP与用户进行交互,获取输入并呈现最终信息。 - Configuration Files(配置文件)
创建配置文件来连接动作、视图以及控制器,这些文件分别是struts.xml、web.xml以及struts.properties。
2. 创建项目
记得勾选Generate web.xml deployment descriptor.
目录结构:
3. 导入jar包
将struts-2.3.34文件中的lib文件夹中的一下jar包引入项目的WebContent/WEB-INF/lib文件夹中
- commons-fileupload-1.3.2.jar
- commons-io-2.2.jar
- commons-lang-2.4.jar
- commons-lang3-3.2.jar
- commons-logging-1.1.3.jar
- freemarker-2.3.22.jar
- javassist-3.11.0.GA
- ognl-3.0.21.jar
- struts2-core-2.3.34.jar
- xwork-core.2.3.34.jar
4. 创建HelloWorldAction类
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; } }
5. 创建HelloWorld.jsp页面
在/Webcontent/下创建jsp文件夹,将所有创建的jsp页面存储在这个文件夹,在/Webcontent/jsp/文件夹下创建HelloWorld.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="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> Hello World, <s:property value="name"/> </body> </html>
6. 在/WebContent/jsp/页面创建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="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</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>
7. 在src目录下创建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> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"> <result name="success">/jsp/HelloWorld.jsp</result> </action> </package> </struts>
8. 在src目录下创建logging.properties配置文件
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO org.apache.catalina.core.ContainerBase.[Catalina].handlers = \ java.util.logging.ConsoleHandler
9. 在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>HelloWorld</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>/jsp/index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> </web-app>
10. 运行项目
在输入框中输入文字,点击Say Hello
11. 设置索引
修改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> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="index"> <result>/jsp/index.jsp</result> </action> <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"> <result name="success">/jsp/HelloWorld.jsp</result> </action> </package> </struts>
在浏览器输入http://localhost:8080/HelloWorld/index.action
即可访问.
3. 配置文件
1). 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> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="index"> <result>/jsp/index.jsp</result> </action> <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"> <result name="success">/jsp/HelloWorld.jsp</result> </action> <!-- more actions can be listed here --> </package> <!-- more packages can be listed here --> </struts>
- <package>标签具有以下属性:
属性 | 描述 |
---|---|
name(必需) | 为package的唯一标识 |
extends | 指定package继承另一package的所有配置。通常情况下,我们使用struts-default作为package的基础。 |
abstract | 定义package为抽象的。如果标记为true,则package不能被最终用户使用。 |
namespace | Actions的唯一命名空间 |
<constant>标签以及name和value属性将用于覆盖default.properties中定义的任一属性,就像我们设置的struts.devMode属性一样。设置struts.devMode属性允许我们在日志文件中查看更多的调试消息。
Results(结果)确定在执行操作后返回到浏览器的内容,而从操作返回的字符串应该是结果的名称。 Results按上述方式配置,或作为“全局”结果配置,可用于包中的每个操作。 Results有
name
和type
属性可选,默认的name值是“success”。Struts.xml文件可以随着时间的推移而增长,因此通过包打破它是使它模块化的一种方式,但struts提供了另一种模块化struts.xml文件的方法,你可以将文件拆分为多个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="my-struts1.xml"/> <include file="my-struts2.xml"/> </struts>
2). struts.properties文件
配置文件提供了一种机制来改变框架的默认行为。
### When set to true, Struts will act much more friendly for developers struts.devMode = true ### Enables reloading of internationalization files struts.i18n.reload = true ### Enables reloading of XML configuration files struts.configuration.xml.reload = true ### Sets the port that the server is run on struts.url.http.port = 8080
4. 拦截器
1). 分类
序号 | 拦截器和说明 |
---|---|
1 | alias 允许参数在请求之间使用不同的别名。 |
2 | checkbox 通过为未检查的复选框添加参数值false,以辅助管理复选框。 |
3 | conversionError 将字符串转换为参数类型的错误信息放置到action的错误字段中。 |
4 | createSession 自动创建HTTP会话(如果尚不存在)。 |
5 | debugging 为开发人员提供一些不同的调试屏幕。 |
6 | execAndWait 当action在后台执行时,将用户发送到中间的等待页面。 |
7 | exception 映射从action到结果抛出的异常,允许通过重定向自动处理异常。 |
8 | fileUpload 便于文件上传。 |
9 | i18n 在用户会话期间跟踪选定的区域。 |
10 | logger 通过输出正在执行的action的名称提供简单的日志记录。 |
11 | params 设置action上的请求参数。 |
12 | prepare 这通常用于执行预处理工作,例如设置数据库连接。 |
13 | profile 允许记录action的简单分析信息。 |
14 | scope 在会话或应用程序范围内存储和检索action的状态。 |
15 | ServletConfig 提供可访问各种基于servlet信息的action。 |
16 | timer 以action执行时间的形式提供简单的分析信息。 |
17 | token 检查action的有效性,以防止重复提交表单。 |
18 | validation 提供action的验证支持。 |
2). 使用Struts2中的拦截器
I. 给HelloWorldAction 添加timer拦截器(测量执行action方法所需的时间)及params拦截器(将请求参数发送给action)
<!-- 根标记元素 --> <struts> <!-- 开发模式-默认为false --> <constant name="struts.devMode" value="true" /> <!-- 声明不同的包, 标签允许配置的分离和模块化 --> <package name="helloworld" extends="struts-default"> <action name="index"> <result>/jsp/index.jsp</result> </action> <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"> <!-- 设置action上的请求参数 --> <interceptor-ref name="params"/> <!-- 以action执行时间的形式提供简单的分析信息 --> <interceptor-ref name="timer"/> <result name="success">/jsp/HelloWorld.jsp</result> </action> <!-- more actions can be listed here --> </package> <!-- more packages can be listed here --> </struts>
II. 测试
在最后两行打印了当前action执行的时间。
3). 自定义拦截器
I. 创建拦截器类
public class MyInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("Pre-Processing--先执行"); // 执行方法 String result = invocation.invoke(); System.out.println("Post-Processing--后执行"); return result; } }
II. 创建Action类
public class HelloWorldAction extends ActionSupport{ private String name; public String execute() throws Exception{ System.out.println("execute..."); return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
III. structs.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> <!-- 开发模式-默认为false --> <constant name="struts.devMode" value="true" /> <!-- 声明不同的包, 标签允许配置的分离和模块化 --> <package name="helloworld" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.mazaiting.struct2.MyInterceptor"/> </interceptors> <action name="index"> <result>/jsp/index.jsp</result> </action> <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"> <!-- 设置action上的请求参数 --> <interceptor-ref name="params"/> <!-- 以action执行时间的形式提供简单的分析信息 --> <interceptor-ref name="timer"/> <!-- 自定义拦截器 --> <interceptor-ref name="myInterceptor"/> <result name="success">/jsp/HelloWorld.jsp</result> </action> <!-- more actions can be listed here --> </package> <!-- more packages can be listed here --> </struts>
IV. 执行结果
5. 文件上传
注意:服务器可能有适当的安全策略,禁止你写入临时目录以外的目录以及属于Web应用程序的目录。使用FileUpload过滤器
1). 创建jsp文件
I. 文件上传fileupload.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="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>File Upload</title> </head> <body> <form action="upload" method="post" enctype="multipart/form-data"> <label for="myFile">Upload your file</label> <input type="file" name="myFile"/> <input type="submit" value="upload"/> </form> </body> </html>
II. 创建上传成功success.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="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>File Upload Success</title> </head> <body> You have successfully uploaded <s:property value="myFileFileName"/> </body> </html>
III. 创建上传失败error.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="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>File Upload Error</title> </head> <body> There has been an error in uploading the file. </body> </html>
2). 创建FileUploadAction
/** * 默认情况下,FileUpload拦截器为你提供三个参数,它们分别按以下方式命名: * [文件名参数] - 这是用户已上传的实际文件。在这个例子中它将是“myFile” * [文件名参数]ContentType - 这是上传的文件的内容类型。在这个例子中,它将是“myFileContentType” * [文件名参数]FileName - 这是上传的文件的名称。在这个例子中,它将是“myFileFileName” * @author mazaiting */ public class FileUploadAction extends ActionSupport{ private File myFile; private String myFileContentType; private String myFileFileName; private String destPath; @Override public String execute() throws Exception { destPath = "E:/file/"; try { System.out.println("Src File name: " + myFile); System.out.println("Dst File name: " + myFileFileName); File destFile = new File(destPath, myFileFileName); FileUtils.copyFile(myFile, destFile); } catch (Exception e) { e.printStackTrace(); return ERROR; } return SUCCESS; } public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } public String getMyFileContentType() { return myFileContentType; } public void setMyFileContentType(String myFileContentType) { this.myFileContentType = myFileContentType; } public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } public String getDestPath() { return destPath; } public void setDestPath(String destPath) { this.destPath = destPath; } }
3). 配置文件
序号 | 属性和说明 |
---|---|
1 | struts.multipart.maxSize 可接受的上传文件的最大值(以字节为单位),默认值为250M。 |
2 | struts.multipart.parser 用于上传多部分表单的库,默认为jakarta。 |
3 | struts.multipart.saveDir 存储临时文件的位置,默认是javax.servlet.context.tempdir。 |
<?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> <!-- 开发模式-默认为false --> <constant name="struts.devMode" value="true" /> <!-- 可接受的上传文件的最大值(以字节为单位),默认值为250M --> <constant name="struts.multipart.maxSize" value="1000000"/> <!-- 声明不同的包, 标签允许配置的分离和模块化 --> <package name="helloworld" extends="struts-default"> <interceptors> <interceptor name="myInterceptor" class="com.mazaiting.struct2.MyInterceptor"/> </interceptors> <action name="index"> <result>/jsp/index.jsp</result> </action> <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"> <!-- 设置action上的请求参数 --> <interceptor-ref name="params"/> <!-- 以action执行时间的形式提供简单的分析信息 --> <interceptor-ref name="timer"/> <!-- 自定义拦截器 --> <interceptor-ref name="myInterceptor"/> <result name="success">/jsp/HelloWorld.jsp</result> </action> <!-- more actions can be listed here --> <action name="upload" class="com.mazaiting.struct2.FileUploadAction"> <!-- 注意过滤器的顺序 --> <interceptor-ref name="fileUpload"> <param name="maximumSize">500000000</param> <param name="allowedTypes">image/jpeg,image/gif,text/plain,application/vnd.ms-powerpoint</param> <param name="allowedExtensions">.txt,.ppt,.jpeg,.jpg,.gif</param> </interceptor-ref> <interceptor-ref name="basicStack"/> <result name="success">/jsp/success.jsp</result> <result name="error">/jsp/error.jsp</result> </action> </package> <!-- more packages can be listed here --> </struts>
fileUpload拦截器有两个参数:maximumSize和allowedTypes。
- maximumSize参数是设置所允许的文件大小的最大值(默认约为2MB)。
- allowedTypes参数是所允许的内容(MIME)类型的用逗号分隔的列表
4). 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>文件上传</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>/jsp/fileupload.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> </web-app>
5). 执行测试
点击upload后
6. 全局异常映射
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> <!-- 开发模式-默认为false --> <constant name="struts.devMode" value="true" /> <!-- 可接受的上传文件的最大值(以字节为单位),默认值为250M --> <constant name="struts.multipart.maxSize" value="1000000"/> <!-- 声明不同的包, 标签允许配置的分离和模块化 --> <package name="helloworld" extends="struts-default"> <!-- interceptors --> <interceptors> <interceptor name="myInterceptor" class="com.mazaiting.struct2.MyInterceptor"/> </interceptors> <!-- global-exception-mappings --> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.NullPointerException"/> </global-exception-mappings> <!-- action* --> <action name="index"> <result>/jsp/index.jsp</result> </action> <action name="hello" class="com.mazaiting.struct2.HelloWorldAction" method="execute"> <!-- 设置action上的请求参数 --> <interceptor-ref name="params"/> <!-- 以action执行时间的形式提供简单的分析信息 --> <interceptor-ref name="timer"/> <!-- 自定义拦截器 --> <interceptor-ref name="myInterceptor"/> <result name="success">/jsp/HelloWorld.jsp</result> </action> <!-- more actions can be listed here --> <action name="upload" class="com.mazaiting.struct2.FileUploadAction"> <!-- 注意过滤器的顺序 --> <interceptor-ref name="fileUpload"> <param name="maximumSize">500000000</param> <param name="allowedTypes">image/jpeg,image/gif,text/plain,application/vnd.ms-powerpoint</param> <param name="allowedExtensions">.txt,.ppt,.jpeg,.jpg,.gif</param> </interceptor-ref> <interceptor-ref name="basicStack"/> <result name="success">/jsp/success.jsp</result> <result name="error">/jsp/error.jsp</result> </action> </package> <!-- more packages can be listed here --> </struts>
6. 注解
1). 添加jar包
在之前导入包的基础上新增antlr-2.7.2.jar、asm.3.3.jar、asm-commons-3.3.jar和struts2-convention-plugin-2.3.34.jar
2). 创建视图
I. 创建主页
<%@ 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="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Employee Form</title> </head> <body> <s:form action="empinfo" method="post"> <s:textfield name="name" label="Name" size="20"/> <s:textfield name="age" label="Age" size="20"/> <s:submit name="submit" label="Submit" align="center"/> </s:form> </body> </html>
II. 创建成功视图
<%@ 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="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Success</title> </head> <body> Employee Information is captured successfully. </body> </html>
III. 创建失败视图
<%@ 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="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Error</title> </head> <body> Employee Information is captured error. </body> </html>
3). 创建Action
@Results({ @Result(name="success", location="/jsp/success.jsp"), @Result(name="error", location="/jsp/error.jsp"), @Result(name="input", location="/jsp/index.jsp") }) public class EmployeeAction extends ActionSupport{ private String name; private int age; @Action(value="/empinfo") @Override public String execute() throws Exception { return SUCCESS; } @RequiredFieldValidator(message="The name is required.") public String getName() { return name; } public void setName(String name) { this.name = name; } @IntRangeFieldValidator(message="Age must be in between 28 and 65", min = "29", max = "65") public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
4). 配置文件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>StrutsAnnoTest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>/jsp/index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>struts.devMode</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
5. 测试
6. 项目结构图

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
SqlMap 初尝试
SqlMap 初尝试 什么是SqlMap Sqlmap是开源的自动化SQL注入工具,由Python写成,具有如下特点: 完全支持MySQL、Oracle、PostgreSQL、Microsoft SQL Server、Microsoft Access、IBM DB2、SQLite、Firebird、Sybase、SAP MaxDB、HSQLDB和Informix等多种数据库管理系统。 完全支持布尔型盲注、时间型盲注、基于错误信息的注入、联合查询注入和堆查询注入。 在数据库证书、IP地址、端口和数据库名等条件允许的情况下支持不通过SQL注入点而直接连接数据库。 支持枚举用户、密码、哈希、权限、角色、数据库、数据表和列。 支持自动识别密码哈希格式并通过字典破解密码哈希。 支持完全地下载某个数据库中的某个表,也可以只下载某个表中的某几列,甚至只下载某一列中的部分数据,这完全取决于用户的选择。 支持在数据库管理系统中搜索指定的数据库名、表名或列名 简单来说SqlMap就是一中SQL注入的工具,能实现对一些网站的SQL注入和获取数据库信息。 如何安装SqlMap 因为SqlMap是基于pytho...
- 下一篇
5月3日云栖精选夜读丨寒武纪重磅发布首款AI云芯片,阿里专家告诉你必须注意的Java编程细节
今天,寒武纪发布最新一代云端AI芯片MLU100和板卡产品,可以与终端处理器完美适配协同完成复杂的智能处理任务。正值五四青年节,云栖社区邀请阿里云NoSQL领域专家,分享90后小鲜肉从实习生成长为专家的故事。还有,阿里专家教你必须注意的Java编程细节...尽在今日云栖夜读! 热点热议 寒武纪重磅发布首款AI云芯片,陈天石要让端云结合占领10亿智能终端! 今天,寒武纪发布第三代IP产品Cambricon 1M和最新一代云端AI芯片MLU100和板卡产品。MLU100云端芯片不仅可独立完成各种复杂的云端智能任务,更可以与寒武纪1A/1H/1M系列终端处理器完美适配,让终端和云端在统一的智能生态基础上协同完成复杂的智能处理任务。 90后实习生,是如何成长为阿里云分布式NoSQL领域专家 我是亦征,本名王怀远,现在是阿里云存储服务团队的研发,正值五四青年节,受云栖社区邀请,来分享下自己的成长故事。从5年前第一次进入阿里云实习到如今,我一直都在表格存储TableStore团队,参与分布式NoSQL的研发等工作。 阿里专家与你分享:你必须注意的Java编程细节 本文主要关注如何在Java中操作一...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Hadoop3单机部署,实现最简伪集群
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Mario游戏-低调大师作品
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果