먼저 일치하는 환경을 게시하겠습니다.
구성:
Eclipse4.3.2
JDK1.7_45
MySQL 5.0+
그런 다음 요점에 도달하십시오.
1. login.jsp
주로 OGNL 태그를 사용하거나 HTML 양식 양식을 사용하여 loginaction.action을 호출하고 게시물에서 전송할 수 있습니다.
loginaaction의 판단 후, <s : fielderror/>를 사용하여 표시 해야하는 신속한 메시지가 있습니다.
<%@ taglib uri = "/struts-tags"prefix = "s"%> <! doctype html public "-// w3c // dtd html 4.01 transitional // en" "http://www.w.w3.org/tr/html4/loose.dtd"> <html> <html> <html>. http-equiv = "content-type"content = "text/html; charset = utf-8"> <title> 로그인 </title> </head> <body> <center> 로그인 <!-addDactionError ( "사용자 이름 또는 비밀번호가 잘못 잡힌다!"); action html의 디스플레이 정보는 Ognl 표현식을 참조해야합니다-> <s : actionerror /> <%-<s : fielderror />-%> <s : form action = "loginaction.action"method = "post"> <s : label value = "username :" /> <s : textfield name = "username" /> <s : labal valess = " /> <s anusterpd =" ":": <sustern = "": ":"username : "username. /> <br /> <s : value = "loginaction" /> < /s : form> < /center> < /body>
2. struts.xml
구성 <br /> 네임 스페이스는 " /", "struts-default"를 상속합니다.
성공적으로 로그인하고 index.jsp로 이동하십시오
로그인 실패, 로그인 .jsp가 반환됩니다
<pre name = "code"> <? 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"> <! 기본. struts2가 처리되며 더 형식화됩니다. 당신이 그것을 참조하지 않으면, 당신은이 속성을 추가 할 수 있습니다-> <constant name = "struts.ui.theme"value = "simple"/> <package name = "default"namespace = "/"struts-default "> <action name ="loginaction "> <result name ="succes ">/index.jsp </result name = <result> <intput"> gogin.jsp> </rection ">. </struts>
3. loginaction.java
로그인
ActionSupport 메서드를 상속하고 Execute () 및 validate () 메소드를 재정의합니다.
방법 실행 메소드 호출 백그라운드에서 호출 된 데이터베이스의 값을 호출하십시오.
Validate 메소드는 사용자 이름과 비밀번호 입력이 비어 있는지 확인하는 데 사용되며 메소드가 Login.jsp에서 호출되었음을 상기시킵니다. strtus.xml로 설정하지 않는 한 설정 정보를 기본적으로 직접 호출 할 수 있습니다.
패키지 com.tikitoo.action; com.opensymphony.xwork2.actionSupport 가져 오기; import com.tikitoo.service.userinfoservice; import com.tikitoo.service.userinfoserviceimpl; / ** * @author tikitoo1 * @see com.opensymphony.xwork2.actionSupport * @see com.opensymphony.xwork2.actionSupport */ public class loginaction actionSupport {private static long serialversionUID = -47605616021545441L; / *** struts2 기본 호출 방법* @return struts2 결과 반환 값*/ @override public string execute () rows exception {userInfoservice userInfoservice = new userInfoserviceImpl (); 부울 플래그 = userinfoservice.loginbyusernameanduserpwd (username, userpwd); 문자열 msg = ""; if (flag == true) {this.addfielderror ( "true", "로그인"); msg = "성공"; } else {this.addfieldnerror ( "사용자 이름 또는 비밀번호가 잘못되었습니다!"); msg = "입력"; } return msg; } // execute () end/ *** 로그인 검증* actactupport 메소드를 다시 작성*/ @override public void validate () {// 사용자 이름이 비어 있는지 결정합니다. } // 비밀번호가 비어 있는지 (getUserPwd () == null || "".Equals (getUserPwd (). trim ())) {this.addfielderror ( "userpwd", "password beyt a vimption"); }} // validate () 끝 개인 문자열 팁; 공개 문자열 getTip () {return 팁; } 개인 문자열 사용자 이름; 개인 문자열 userpwd; public String getUserName () {return username; } public void setusername (String username) {this.username = username; } public String getUserPwd () {return userPwd; } public void setUserpwd (String userPwd) {this.userpwd = userpwd; }}사용자 이름과 비밀번호는 잘못 입력됩니다.
사용자 이름이 올바르게 입력되면 로그인이 성공합니다.
위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.