struts2 简单的登录功能
学了一定的jsp基础之后,我们可以试着去尝试去实现一个简单的struts2的动态登录功能,具体代码如下:
一、目录结构
首先,lib中导入struts2所需的核心包,
下载地址:http://struts.apache.org/download.cgi#struts23281
总体目录结构如下:
二、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>srtuts2</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>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>
三、创建一个用户类User.java
package com.ruanjian.model; public class User { private String userName; private String passworld; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassworld() { return passworld; } public void setPassworld(String passworld) { this.passworld = passworld; } }
四、创建login.jsp文件,用于登录的主页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>struts2登录页面</title> </head> <body> <form action = "login" method = "post" > 用户名:<input type = "text" name = "userName" /> 密码:<input type = "text" name = "passworld" /> <input type = "submit" value = "登录" /> </form> </body> </html>
创建success.jsp文件,代表登录成功页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>登陆成功</title> </head> <body> 登陆成功! </body> </html>
创建error.jsp文件,代表登录失败页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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=utf-8"> <title>登录失败</title> </head> <body> 登录失败! </body> </html>
五、创建UserService.java用于验证用户登录
package com.ruanjian.service; import com.ruanjian.model.User; public class UserService { public boolean isLogin(User user){ boolean flag = false; if("admin".equals(user.getUserName()) && "123456".equals(user.getPassworld())){ flag = true; } return flag; } }
六、创建LoginAction.java文件,后台处理登录
package com.ruanjian.action; import com.opensymphony.xwork2.Action; import com.ruanjian.model.User; import com.ruanjian.service.UserService; /** * struts2登录后台处理 * @author * */ public class LoginAction implements Action { UserService userService = new UserService(); private String userName; private String passworld; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassworld() { return passworld; } public void setPassworld(String passworld) { this.passworld = passworld; } @Override public String execute() throws Exception { User user = new User(); user.setUserName(userName); user.setPassworld(passworld); if(userService.isLogin(user)){ return SUCCESS; //如果登录成功,返回成功 }else{ return ERROR; //否则返回失败 } }
七、新建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> <package name = "index" extends = "struts-default"> <action name = "login" class = "com.ruanjian.action.LoginAction"> <result name = "success">/success.jsp</result> <result name = "error">/error.jsp</result> </action> </package> </struts>