struts2登录注册示例_Struts 2动作示例教程

struts2登录注册示例

If you are working on Struts 2, you will spent a lot of time developing Struts 2 Actions. This article is aimed to provide you more details about Struts 2 Action classes and different ways to implement them.

如果您正在使用Struts 2,则将花费大量时间来开发Struts 2 Actions 。 本文旨在为您提供有关Struts 2 Action类以及实现它们的不同方法的更多详细信息。

Struts 2动作概述 (Struts 2 Action Overview)

Struts 2 Action is at the front of our application that takes care of handling client requests. You will notice that in any struts 2 application, there is an action class associated with different type of client action.

Struts 2 Action在我们应用程序的最前面,负责处理客户请求。 您会注意到,在任何Struts 2应用程序中,都有一个与不同类型的客户端操作关联的操作类。

Struts 2 Action class does following tasks:

Struts 2 Action类执行以下任务:

  1. Business Logic: Our application business logic resides in action classes. If your application has a lot of business logic, you might have other helper classes to do the actual work but it’s always Action class that integrates them with the application.

    业务逻辑 :我们的应用程序业务逻辑位于操作类中。 如果您的应用程序具有大量业务逻辑,则可能还有其他帮助程序类可以完成实际工作,但是始终是Action类将它们与应用程序集成在一起。
  2. Data Carrier: If you look into any Servlet code, you will notice that it’s our responsibility to get the client request data and process them. Most of the times we get the request data to create a java bean object and then invoke our business logic classes. Struts 2 Action classes makes our life easier with handling the data mapping to action class bean properties or to another java bean internally. It saves a lot of code that we used to write with Servlets. Similarly Struts Action takes care of the process to make sure result pages have access to the data that can be used in generating client response.

    Actually this task is done by Struts 2 params interceptor defined in struts-default package as:

    <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

    数据载体 :如果您查看任何Servlet代码,您将注意到获取客户请求数据并对其进行处理是我们的责任。 大多数时候,我们获取请求数据以创建一个Java bean对象,然后调用我们的业务逻辑类。 Struts 2动作类通过处理到动作类Bean属性或内部另一个Java Bean的数据映射,使我们的生活更加轻松。 它节省了许多我们以前用Servlet编写的代码。 同样,Struts Action负责处理过程,以确保结果页面可以访问可用于生成客户端响应的数据。

    实际上,此任务是由Struts-default包中定义的Struts 2 params拦截器完成的:

    <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

  3. Determining Result: Struts 2 Action classes are responsible for determining the result pages that will be used for generating client response. It can be simple HTML page, JSP page etc.

    确定结果 :Struts 2 Action类负责确定将用于生成客户端响应的结果页面。 它可以是简单HTML页面,JSP页面等。

There are four ways through which we can create Struts 2 Action classes.

我们可以通过四种方式创建Struts 2 Action类。

  1. Simple Action Class: We can use any normal java class as Struts 2 action class, the only requirement is that it should have execute() method returning String. In the simplest form, we can have action class as:
    package com.journaldev.struts2.action;
    
    public class HomeAction {
    
    	public String execute(){
    		return "success";
    	}
    }

    Now we can configure this in struts.xml file as:

    Now we can access this action with URL https://localhost:8080/Struts2ActionExample/home.action and it will return home.jsp page as response.

    Struts 2 default location for result pages are WEB-INF/content and we are overriding it with struts.convention.result.path configuration, read more about this at Struts 2 ResultPath.

    简单动作类 :我们可以使用任何普通的java类作为Struts 2动作类,唯一的要求是它必须具有execute()方法来返回String。 在最简单的形式中,我们可以将动作类设置为:
    package com.journaldev.struts2.action;
    
    public class HomeAction {
    
    	public String execute(){
    		return "success";
    	}
    }

    现在,我们可以在struts.xml文件中将其配置为:

    现在,我们可以使用URL https://localhost:8080/Struts2ActionExample/home.action访问此操作,它将返回home.jsp页面作为响应。

    结果页面的Struts 2默认位置是WEB-INF / content ,我们正在使用struts.convention.result.path配置覆盖它,请在Struts 2 ResultPath上阅读有关此内容的更多信息。

  2. Implementing Action interface: We can implement com.opensymphony.xwork2.Action interface also to create action classes. Action interface has a single method execute() that we need to implement. The only benefit of using Action interface is that it contains some constants that we can use for result pages, these constants are SUCCESS, ERROR, NONE, INPUT and LOGIN. We can rewrite above HomeAction class by implementing Action interface as shown below.
    package com.journaldev.struts2.action;
    
    import com.opensymphony.xwork2.Action;
    
    public class HomeAction implements Action{
    
    	public String execute(){
    		return SUCCESS;
    	}
    }

    Read more about this approach at Struts 2 Beginners Tutorial.

    实现Action接口 :我们还可以实现com.opensymphony.xwork2.Action接口来创建Action类。 动作接口具有我们需要实现的单个方法execute()。 使用Action接口的唯一好处是它包含一些可用于结果页的常量,这些常量是SUCCESS,ERROR,NONE,INPUT和LOGIN。 我们可以通过实现如下所示的Action接口来重写HomeAction类。

    Struts 2初学者教程中了解有关此方法的更多信息。

  3. Using Struts 2 Annotations: Struts 2 supports annotation based configuration and we can use it to create action classes. To use struts 2 annotations, we need to add struts2-convention-plugin library in the classpath and in web.xml struts2 filter configuration provide the java package that contains action classes.
    <filter>
    	<filter-name>struts2</filter-name>
    	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    	<init-param>
    		<param-name>actionPackages</param-name>
    		<param-value>com.journaldev.struts2.action</param-value>
    	</init-param>
    </filter>

    We can rewrite HomeAction class using annotation as below.

    Read more about Struts 2 annotations at Struts 2 Annotations Hello World Example.

    使用Struts 2注释 :Struts 2支持基于注释的配置,我们可以使用它来创建动作类。 要使用struts 2批注,我们需要在类路径中添加struts2-convention-plugin库,并在web.xml中的struts2过滤器配置提供包含动作类的java包。
    <filter>
    	<filter-name>struts2</filter-name>
    	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    	<init-param>
    		<param-name>actionPackages</param-name>
    		<param-value>com.journaldev.struts2.action</param-value>
    	</init-param>
    </filter>

    我们可以使用注解重写HomeAction类,如下所示。

    Struts 2 Annotations Hello World示例中阅读有关Struts 2注解的更多信息。

  4. Extending ActionSupport Class: ActionSupport class is the default implementation of Action interface and it also implements interfaces related to Validation and i18n support. ActionSupport class implements Action, Validateable, ValidationAware, TextProvider and LocaleProvider interfaces.

    We can override validate() method of ActionSupport class to include field level validation login in our action classes. For example, if we have a JSP page login.jsp as below.

    login.jsp

    <%@ page language="java" contentType="text/html; charset=US-ASCII"
        pageEncoding="US-ASCII"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
    <%-- Using Struts2 Tags in JSP --%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
    <title>Login Page</title>
    </head>
    <body>
    <h3>Welcome User, please login below</h3>
    <s:form action="login">
    	<s:textfield name="name" label="User Name"></s:textfield>
    	<s:textfield name="pwd" label="Password" type="password"></s:textfield>
    	<s:submit value="Login"></s:submit>
    </s:form>
    </body>
    </html>

    Then we can have LoginAction class with form fields validation as:

    We can configure this action class as:

    <action name="login" class="com.journaldev.struts2.action.LoginAction">
    	<result name="SUCCESS">welcome.jsp</result>
    	<result name="input">login.jsp</result>
    </action>

    Now if either of the fields name or pwd will be empty in request, addFieldError() method will add an error message and struts 2 will return the response with result name “input”. You will get the login.jsp as response with error messages mentioned in the page.

    扩展ActionSupport类 :ActionSupport类是Action接口的默认实现,并且它还实现与Validation和i18n支持有关的接口。 ActionSupport类实现Action,Validateable,ValidationAware,TextProvider和LocaleProvider接口。

    我们可以重写ActionSupport类的validate()方法以在我们的操作类中包括字段级验证登录。 例如,如果我们有一个如下所示的JSP页面login.jsp。

    login.jsp

    然后我们可以使用表单字段验证的LoginAction类为:

    package com.journaldev.struts2.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport {
    
    	private static final long serialVersionUID = -1273531583006208372L;
    	
    	@Override
    	public String execute(){
    		return SUCCESS;
    	}
    	
    	@Override
    	public void validate(){
    		if("".equals(getName())){
    			addFieldError("name", "UserName can't be empty");
    		}
    		if("".equals(getPwd())){
    			addFieldError("pwd", "Password can't be empty");
    		}
    	}
    	
    	//Java Bean to hold the form parameters
    		private String name;
    		private String pwd;
    		public String getName() {
    			return name;
    		}
    		public void setName(String name) {
    			this.name = name;
    		}
    		public String getPwd() {
    			return pwd;
    		}
    		public void setPwd(String pwd) {
    			this.pwd = pwd;
    		}
    }

    我们可以将此操作类配置为:

    现在,如果请求中的字段名称或pwd中的任何一个为空,则addFieldError()方法将添加一条错误消息,并且struts 2将返回结果名称为“ input”的响应。 您将获得login.jsp作为响应,并带有页面中提到的错误消息。

Depending on your requirements, you can use any of the approaches to create struts 2 action classes, my favorite is ActionSupport class because it helps in writing validation and i18n logic easily in action classes.

根据您的需求,您可以使用任何方法来创建struts 2动作类,我最喜欢的是ActionSupport类,因为它有助于在动作类中轻松编写验证和i18n逻辑。

翻译自: https://www.journaldev.com/2173/struts-2-actions-example-tutorial

struts2登录注册示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值