Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize user login by WebWork

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "WebWork how to achieve user login", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "WebWork how to achieve user login" this article.

The main functions of WebWork user login are as follows:

1. Name,password field in login.jsp

2. User name and password cannot be empty! If prompted empty (using Webwork authentication, internationalization)

2. When the user name is: xiaomaha password 123, jump to the seccess.jsp page, otherwise jump to the defeat.jsp page

Log in to WebWork users * * step: guide package

Version: webwork-2.2.5

Http://www.opensymphony.com/webwork

You can download the package you need.

1 、 webwork-2.2.5.jar

2. All packages in default under lib directory

Step 2 of WebWork user login: configure web.xml

Com.opensymphony.webwork.dispatcher.FilterDispatcher is the Servlet controller in WebWork

< web-appversion= "2.4" xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > < filter > < filter-name > Webwork < / filter-name > < filter-class > com.opensymphony.webwork.dispatcher.FilterDispatcher < / filter-class > < / filter > < filter-mapping > < filter-name > webwork < / filter-name > < url-pattern > * .action < / url-pattern > < / filter-mapping > < / web-app >

Filter is a filter, and requests for * .action on the page are sent to the servlet controller of WebWork.

WebWork user login step 3: create an Action

First, create an Action in Action,WebWork that is actually a normal JAVA class.

Just write an execute () method to return

String type, which is defined in the configuration file. Then it is an Action. Equivalent to the Action in Struts1! WebWork realized decoupling with Servlet, convenient for testing. I like it very much!

But if you need convenient development, then inherit ActionSupport and override its execute (). Note that its return value is a String, not an ActionForword.

Two methods can be used to implement verification in WebWork.

1. Inherit ActionSupport in Action and override the validate () method (I have given comments!)

2, configuration file implementation, this example uses a configuration file, note that the verified configuration file must be written to the same package as the custom Action!

Package com.xiaomaha.action; import com.opensymphony.xwork.ActionSupport; public class LoginAction extends ActionSupport... {private String userName; private String passWord; public String getPassWord ()... {return passWord;} public void setPassWord (String passWord)... {this.passWord = passWord;} public String getUserName ()... {return userName;} public void setUserName (String userName)... {this.userName = userName } public String execute () throws Exception... {String path = null; if ("xiaomaha" .equals IgnoreCase (this.userName) & & "123". Equals (this.passWord)... {path = this.SUCCESS;} else... {path = this.ERROR;} return path } / / public void validate () {/ / if (".equals (this.userName) | | this.userName==null) {/ / * addFieldError () is equivalent to ActionErrors* / / * in Struts1. If you want to achieve internationalization, you need getText (), which is used to read the key in the resource file / / * getText (" name.null "). It will find value / / * / this.addFieldError (" userName ", getText (" name.null ")). / /} / if (".equals (this.passWord) | | this.passWord==null) {/ / this.addFieldError (" passWord ", getText (" pass.null ")); / /} / /}}

Step 4 of logging in to WebWork users: create xwork.xml

Xwork.xml is a bit like struts-config.xml in Struts1

The xwork.xml file must be placed under the classpath under WEB-INF/classes/

WebWork's Servlet controller will find it and parse it, the path must not be misplaced, the name must not be wrong!

Also note that all Action tags in WebWork must be under the package tag. Package is the parent tag of action.

< include file= "webwork-default.xml" > < / include > will find the webwork-default.xml in the JAR file in WebWork Must write < xwork >

Share To

Development

© 2024 shulou.com SLNews company. All rights reserved.

12
Report