直接贴后台代码:
package com.struts2.action; import java.util.HashMap; import java.util.Locale; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.struts2.localeutil.LocaleUtil; import com.struts2.model.User; import com.sun.org.apache.xml.internal.utils.LocaleUtility; public class UserAction extends ActionSupport{ private static final long serialVersionUID = -1905893698092880349L; private final Map<String,Locale> localMap = new HashMap<String, Locale>(); private String id; private String username; private String password; private String language; private Map<String, Locale> langugeMap; public UserAction(){ this.langugeMap = new HashMap<String, Locale>(); this.setLangugeMap(getcurrLocalMapMethod()); } public Map<String, Locale> getLangugeMap() { return langugeMap; } public void setLangugeMap(Map<String, Locale> langugeMap) { this.langugeMap = langugeMap; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Map<String,Locale> getcurrLocalMapMethod(){ localMap.put("简体中文", Locale.CHINA); localMap.put("English", Locale.ENGLISH); localMap.put("繁体中文", Locale.TAIWAN); localMap.put("日本の言語",Locale.JAPAN); localMap.put("Americanese English", Locale.US); return localMap; } /** * 登陆方法; * @return String; */ @SuppressWarnings("unchecked") public String login(){ this.id = "1001"; //判断; if(this.username.equals("zhouhaitao") && this.password.equals("123456")){ //ActionContext.getContext.相当于request.setAttribuet()一样; ActionContext.getContext().put("id",id); //保存到session当中; ActionContext.getContext().getSession().put("name", this.username+"登陆成功!"); return "yes"; }else{ return "no"; } } /** * 注册方法; */ public String regester(){ System.out.println("调用注册的方法.开始注册!"); System.out.println(this.id); System.out.println(this.username); System.out.println(this.password); ActionContext.getContext().put("name",this.username+"注册成功!"); return "yes"; } /** * 获取选择的语言; * @return */ public String getSelectLanguge(){ System.out.println("获取的语言是:"+this.language); String[] str= this.language.split("_"); Locale locale2 = null; Locale locale1 = null; if(str.length>2){ locale1 = LocaleUtility.langToLocale(this.language.split("_")[1]); locale2 = LocaleUtil.toLocale(this.language.split("_")[1]); }else{ locale1 = LocaleUtility.langToLocale(this.language.split("_")[0]); locale2 = LocaleUtil.toLocale(this.language.split("_")[0]); } System.out.println("1通过转换后的语言是:"+locale1.getLanguage()); System.out.println("2通过转换后的语言是:"+locale2.getLanguage()); return "index"; } }
DemoAction :
package com.struts2.action; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.struts2.model.User; public class DemoAction extends ActionSupport{ private static final long serialVersionUID = 6021481463050081906L; private List<String> stuList; private List<User> userList; private Map<String, User> userMap; private int sid; private String sname; private String address; @SuppressWarnings("unchecked") public String displayStudentList(){ stuList = new ArrayList<String>(); stuList.add("ZhouHaiTao"); stuList.add("LiuJing"); stuList.add("ZengPing"); stuList.add("ZhangSan"); ServletActionContext.getContext().getSession().put("stuList", stuList); ServletActionContext.getContext().put("selectFruit", sname); return "display"; } public String onClickUrl(){ ServletActionContext.getContext().put("urlMsg", "你单击了超链接,提交至Action中!"); return "display"; } public List<User> getUserList() { return userList; } public void setUserList(List<User> userList) { this.userList = userList; } public Map<String, User> getUserMap() { return userMap; } public void setUserMap(Map<String, User> userMap) { this.userMap = userMap; } public List<String> getStuList() { return stuList; } public void setStuList(List<String> stuList) { this.stuList = stuList; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
直接贴前台代码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!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> <script type="text/javascript"> function commitToDemoAction(){ document.getElementById("myform").action="demo!displayStudentList.action"; } </script> </head> <body> <center> <h1>Struts2 标签使用大全</h1> </center> <s:form id="myform" name="myform" method="post" theme="simple"> <b>标签一(s:submit的标签使用)</b><br/> <s:submit id="bid" onclick="commitToDemoAction()" value="提交到Action中获取数据" cssStyle="border:1px solid blue"/><br/><br/> <b>标签二(s:iterator的标签使用)</b><br/> 显示结果如下: <br/> <s:iterator id="stuid" status="sta" value="stuList"> ${stuid } </s:iterator> <br/><br/> <b>标签三(s:if..s:else的标签使用)</b><br/> <s:if test="sid == 10"> sid等于10 </s:if> <s:else> sid不等于10 </s:else> <br/><br/> <b>标签四(s:a的标签使用)</b><br/> <s:a id="sid" cssStyle="color:blue" label="超链接" value="100" href="/Struts2/demo/demo!onClickUrl.action">Struts2超级接的使用</s:a> <font color="red">${request.urlMsg }</font> <br/><br/> <b>标签五(s:bean的标签使用)直接取得后台User对象的值</b><br/> <s:bean name="com.struts2.model.User" id="sbean" /> <table id="tid" border="1px" style="color:blue;width:400px" cellpadding="0px" cellspacing="0px"> <tr> <td>${sbean.userId } </td> <td>${sbean.userName }</td> <td>${sbean.userAge }</td> <td>${sbean.userAddress }</td> </tr> </table> <br/><br/> <b>标签六(s:checkbox的标签使用)struts2的checkbox标签在使用的时候需要注意它的两个属性。value和fieldValue,在其他的标签中value是真实值。而在checkbox中,value如果为true,就表示复选框被选中;如果为false就表示不被选中。fieldValue才是此复选框对应的真实的值</b><br/> 苹果:<s:checkbox fieldValue="苹果" value="false" name="sname"/> 鸭梨:<s:checkbox fieldValue="鸭梨" value="false" name="sname"/> 香蕉: <s:checkbox fieldValue="香蕉" value="false" name="sname"/> 葡萄: <s:checkbox fieldValue="葡萄" value="true" name="sname"/> <br/> 你选择的水果是: <font color="red">${request.selectFruit }</font> </s:form> </body> </html>
Struts2.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> <!-- 可以引用其他的配置文件 --> <include file="struts-default.xml"></include> <!-- name代表包的名称 extends代表引用其他的默认文件,namespace代表命名空间,可选--> <package name="com.struts2.action" extends="struts-default"> <!-- 公用的结果 --> <global-results> <result name="index">/index.jsp</result> </global-results> <!-- name 代表action名称,与struts1的path类似 --> <action name="user" class="com.struts2.action.UserAction"> <result name="yes" type="dispatcher">success.jsp</result> <result name="no" type="redirect">failure.jsp</result> </action> <action name="demo" class="com.struts2.action.DemoAction"> <result name="display" type="dispatcher">/demo/displayData.jsp</result> </action> </package> </struts>