struct2
login.jsp
1 <%@ page language="java" contentType="text/html; charset=GBK" 2 pageEncoding="GBK"%> 3 <%@taglib prefix="s" uri="/struts-tags"%> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=GBK"> 7 <title><s:text name="loginPage"/></title> 8 </head> 9 <body> 10 <s:form action="login"> 11 <s:textfield name="username" key="user"/> 12 <s:textfield name="password" key="pass"/> 13 <s:submit key="login"/> 14 </s:form> 15 </body> 16 </html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title><s:text name="succPage"/></title> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> </head> <body> <s:text name="succTip"> <s:param>${sessionScope.user}</s:param> </s:text><br/> </body> </html>
登陆失败界面error.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title><s:text name="errorPage"/></title> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> </head> <body> <s:text name="failTip"/> </body> </html>
当login.jsp触发action时,就会向后抬发送login.action的请求,这个请求被后台拦截,交给struts.xml中配置的action处理
1 <?xml version="1.0" encoding="GBK"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" 4 "http://struts.apache.org/dtds/struts-2.1.7.dtd"> 5 <struts> 6 <!-- 指定全局国际化资源文件 --> 7 <constant name="struts.custom.i18n.resources" value="mess"/> 8 <!-- 指定国际化编码所使用的字符集 --> 9 <constant name="struts.i18n.encoding" value="GBK"/> 10 <!-- 所有的Action定义都应该放在package下 --> 11 <package name="test" extends="struts-default"> 12 <action name="login" class="com.test.action.LoginAction"> 13 <result name="error">/error.jsp</result> 14 <result name="success">/welcome.jsp</result> 15 </action> 16 </package> 17 </struts>
LoginAction,可以看到成功登陆后,程序把username写入session中。以便于我们在welcome.jsp中利用${sessionScope.user}取得名字
1 package com.test.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 public class LoginAction extends ActionSupport { 7 private String username; 8 private String password; 9 10 public String getUsername() { 11 return username; 12 } 13 14 public void setUsername(String username) { 15 this.username = username; 16 } 17 18 public String getPassword() { 19 return password; 20 } 21 22 public void setPassword(String password) { 23 this.password = password; 24 } 25 26 public String execute() throws Exception { 27 if (getUsername().equals("xingoo") && getPassword().equals("123")) { 28 ActionContext.getContext().getSession().put("user", getUsername()); 29 return SUCCESS; 30 }else{ 31 return ERROR; 32 } 33 } 34 }
这里还要两个国际化文件,
mess.properties
loginPage=loginPage errorPage=errorPage succPage=succPage failTip=sorry,login failed succTip=welcome{0},login success user=username pass=password login=login
mess_zh_CN.properties
loginPage=登陆界面 errorPage=失败界面 succPage=成功界面 failTip=对不起,您不能登录! succTip=欢迎,{0},您已经登录! user=用户名 pass=密 码 login=登陆
登陆界面
登陆成功