標籤:動態 struts 登入
學了一定的jsp基礎之後,我們可以試著去嘗試去實現一個簡單的struts2的動態登入功能,具體代碼如下:
一、目錄結構
首先,lib中匯入struts2所需的核心包,
:http://struts.apache.org/download.cgi#struts23281
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/7F/8E/wKiom1ciJ9-Dy7LGAABJTyL2cvs064.png" title="W$K0(X_W95HHJ%}F$26@~~Y.png" alt="wKiom1ciJ9-Dy7LGAABJTyL2cvs064.png" />
總體目錄結構如下:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/7F/8E/wKiom1ciJ-PD6gzAAABuuVB2keQ353.png" title="J5GT$@INOPPB~E[]UKV~S_G.png" style="float:none;" alt="wKiom1ciJ-PD6gzAAABuuVB2keQ353.png" />
二、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;}@Overridepublic 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>
本文出自 “11510677” 部落格,請務必保留此出處http://11520677.blog.51cto.com/11510677/1768859
struts2 簡單的登入功能