The Struts2 framework enables simple user login and struts2 framework Login

Source: Internet
Author: User

The Struts2 framework enables simple user login and struts2 framework Login

The Struts Framework draws on the advantages of Struts, with WebWork as the core, interceptor, variable and reusable labels.

Step 1: load the Struts2 Class Library:

Step 2: Configure web. xml

<? Xml version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> <! -- Configure the Struts2 filter: intercept the request --> <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> <! -- '/*' Filters all requests --> </filter-mapping> </web-app>

Step 3: Develop the view layer page (submit a single table page)

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> Iv. Three-tier architecture:
Package dao;/*** user operation interface ** @ author Administrator **/public interface UserDao {public String login (String username, String password );}
Package biz;/*** user business interface ** @ author Administrator **/public interface UserBiz {public String login (String username, String password );}
Package biz. impl; import biz. userBiz; import dao. userDao; import dao. userDaoImpl; public class UserBizImpl implements UserBiz {// create the dao layer object UserDao userdao = new UserDaoImpl ();/*** call the dao Layer Method */public String login (String username, string password) {return userdao. login (username, password );}}
package dao;public class UserDaoImpl implements UserDao {    public String login(String username, String password) {        String str = "";        if(username.equals("msit") && password.equals("123456")){            str = "success";        }else{            str = "error";        }                // TODO Auto-generated method stub        return str;    }}

Step 5: development control layer Action

Package Action; import java. util. map; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; import org. apache. struts2.ServletActionContext; import biz. userBiz; import biz. impl. userBizImpl; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport;/*** logon controller * @ author Administrator **/public class LoginAction extends ActionSupport {/* value Stack: The page can be directly obtained */String username; // form name; encapsulate String password; // form name; encapsulate // create the Biz layer object UserBiz userbiz = new UserBizImpl ();/* (non-Javadoc) * @ see com. opensymphony. xwork2.ActionSupport # execute () */@ Override public String execute () throws Exception {// TODO Auto-generated method stub System. out. println (username + "=" + password);/* decoupling (reducing dependencies); Based on the Map set */ActionContext ac = ActionContext. getContext ();/* GET request */Map request = (Map) ac. get ("request"); // request. put ("username", username);/* Get session */Map session = ac. getSession ();/* Get application */Map application = ac. getApplication (); // ================================================ ===================/* coupling (lazy) method */HttpServletRequest request2 = ServletActionContext. getRequest (); HttpServletResponse response = ServletActionContext. getResponse (); HttpSession session2 = request2.getSession (); // request2.setAttribute ("username", username);/*** struts provides five status strings by default */return userbiz. login (username, password);}/*** @ return the username */public String getUsername () {return username ;} /*** @ param username the username to set */public void setUsername (String username) {this. username = username;}/*** @ return the password */public String getPassword () {return password ;} /*** @ param password the password to set */public void setPassword (String password) {this. password = password ;}}

Step 6: Configure struts. xml (Core File); you can refer to struts-default.xml

<? 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> <! -- Configure package information --> <package name = "default" namespace = "/" extends = "struts-default"> <! -- Configure Action: Associate Action JavaBean --> <action name = "LonginAction" class = "Action. LoginAction"> <! -- Specify the returned view. Forwarding is used by default. --> <result name = "success"> success. jsp </result> <result name = "error"> error. jsp </result> </action> </package> </struts>

Step 7: return the corresponding string success and error after processing the logic to jump to the corresponding page.

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

Note:

Configure struts2 1. Add a class library (jar package). copy the file from the application instance. 2. Configure the filter in web. xml. <! -- Configure the Struts2 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 all requests --> </filter-mapping> 3. Compile the page index. jsp 4. Configuration controller (action) LoginAction inherits Actionsupper to override the excute () method of the parent class. 5. Configure struts. xml (Core File); can refer to struts-default.xml 6, after processing the logic returns the corresponding string success, error jump to the corresponding page

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.