JavaEE企业应用实战学习记录struts2登录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaEE企业应用实战学习记录struts2登录相关的知识,希望对你有一定的参考价值。
1 <%-- login.jsp 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2016/10/6 5 Time: 16:26 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%@taglib prefix="s" uri="/struts-tags" %> 10 <html> 11 <head> 12 <title><s:text name="loginPage"/>></title> 13 </head> 14 <body> 15 <s:form action="login"> 16 <s:textfield name="username" key="user"/> 17 <s:textfield name="password" key="pass"/> 18 <s:submit key="login"/> 19 </s:form> 20 </body> 21 </html>
1 package sanglp; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 /** 7 * Created by Administrator on 2016/10/6. 8 *LoginAction 9 */ 10 public class LoginAction extends ActionSupport { 11 12 //定义封装请求参数的username和password属性 13 private String username; 14 private String password; 15 16 //定义处理用户请求的execute方法 17 public String execute(){ 18 if(getUsername().equals("crazyit.org")&&getPassword().equals("leegang")){ 19 ActionContext.getContext().getSession().put("user",getUsername()); 20 return SUCCESS; 21 }else{ 22 return ERROR; 23 } 24 } 25 public String getUsername() { 26 return username; 27 } 28 29 public String getPassword() { 30 return password; 31 } 32 33 public void setUsername(String username) { 34 this.username = username; 35 } 36 37 public void setPassword(String password) { 38 this.password = password; 39 } 40 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 7 <!--指定Struts2配置文件的根元素--> 8 <struts> 9 <!--指定全局国际化资源文件--> 10 <constant name="struts.custom.i18n.resources" value="mess"/> 11 <!--指定国际化编码所使用的字符集--> 12 <constant name="struts.i18n.encoding" value="utf-8"/> 13 <package name="sanglp" extends="struts-default"> 14 <action name="login" class="sanglp.LoginAction"> 15 <!--定义三个逻辑视图和物理资源之间的映射--> 16 <result name="input">/login.jsp</result> 17 <result name="error">/error.jsp</result> 18 <result name="success">/welcome.jsp</result> 19 </action> 20 </package> 21 </struts>
1 <%@ page language="java" contentType="text/html; charset=UTF-8"%> 2 <%@taglib prefix="s" uri="/struts-tags"%> 3 <html> 4 <head> 5 <title><s:text name="succPage"/></title> 6 </head> 7 <body> 8 <s:text name="succTip"> 9 <s:param>${sessionScope.user}</s:param> 10 </s:text><br> 11 </body> 12 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8"%> 2 <%@taglib prefix="s" uri="/struts-tags"%> 3 4 <html> 5 <head> 6 <title><s:text name="errorPage"/></title> 7 </head> 8 <body> 9 <s:text name="failTip"/> 10 </body> 11 </html>
成功页面:
以上是关于JavaEE企业应用实战学习记录struts2登录的主要内容,如果未能解决你的问题,请参考以下文章
JavaEE企业应用实战学习记录authorityFilter
JavaEE企业应用实战学习记录requestListener