您的位置:首页 > 编程语言 > Java开发

JAVA_WEB Struts2学习:Struts2标签学习

2013-08-21 18:15 597 查看
List<String>-----底层就是数组,所以用数组也可以表示:String[]Struts2数据自动类型转换、封装。Core核心每个Struts2标签自成一行,在table下可省略tr,前提是不禁止struts2标签的默认格式生效。Struts2默认是Post请求。解决Struts2中文乱码问题:1.解决Post请求中文乱码:配置Struts2.xml文件。<constant name="struts.i18n.encoding" value="GBK"></constant> <!-- 解决Post请求中文乱码 -->2.解决Get请求中文乱码:步骤1:,然后后退到E:\ProgramFiles (x86)\MyEclipse\apache-tomcat-6.0.33\conf下:打开server文件:修改为:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="GBK"/>
让Struts2的样式失效:配置Struts2.xml文件:
<constant name="struts.ui.theme" value="simple"></constant>
以下是Struts2标签的演示代码:一、index.jsp:
<%@ page language="java" import="java.util.*,com.zuxia.yc42.po.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";Map<Integer,String> map = new HashMap<Integer,String>();	//HashMap造数据1map.put(0,"重庆");map.put(1,"广东");map.put(2,"深圳");map.put(3,"成都");map.put(4,"北京");map.put(5,"上海");request.setAttribute("map",map);List<UserInfo> list = new ArrayList<UserInfo>();			//ArrayList造数据2UserInfo userInfo1 = new UserInfo();userInfo1.setUsId(1);userInfo1.setUsName("小强");userInfo1.setUsAge(18);userInfo1.setUsSex("男");UserInfo userInfo2 = new UserInfo();userInfo2.setUsId(2);userInfo2.setUsName("小离");userInfo2.setUsAge(25);userInfo2.setUsSex("女");list.add(userInfo1);list.add(userInfo2);request.setAttribute("users",list);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>'Struts2Taglib.jsp'标签的学习</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">.fieldStyle{height: 100px;}</style></head><body><!--EL表达式:${scope.key} ${requestScope.map}OGNL表达:#key / #scope.key  #request.map#userInfo.usName          #userInfo['usName']--><s:form action="initTest" method="get" namespace="/userManager"><table border="1" width="90%" align="center"><s:textfield name="usName" id="usName" cssStyle="width:200px;" cssClass="fieldStyle" label="用户名" ></s:textfield><s:password name="usPass" id="usPass" label="密码"></s:password><s:radio list="#{0:'男',1:'女'}" name="usSex" id="usSex" ></s:radio><s:checkboxlist list="#request.map" name="usAH"></s:checkboxlist><s:select list="#request.map" name="usJg"></s:select><s:textfield name="usAge" id="usAge"label="年龄" ></s:textfield><s:submit value="提交"></s:submit><s:reset value="重置"></s:reset></table></s:form><form action="myAction"> <s:debug></s:debug><table><tr><th>序号</th><th>姓名</th><th>年龄</th><th>性别</th></tr><!--JSTL + ELSTRUTS2 + OGNL--><s:iterator value="#attr.users" var="userInfo" status="status">		<!--迭代数据	:#attr.users可以等价于#request.users		--><tr><td><s:property value="#status.index"/></td><td><s:property value="#userInfo.usName"/></td><td><s:property value="#userInfo.usAge"/></td><td><s:if test="usSex == 0">男</s:if><s:else>女</s:else></td></tr></s:iterator></table><input type="submit" value="登陆" /></form></body></html>
二、MyTestAction extends ActionSupport类:
package com.zuxia.yc42.action;import java.util.List;import java.util.Set;import com.opensymphony.xwork2.ActionSupport;import com.zuxia.yc42.po.UserInfo;public class MyTestAction extends ActionSupport{//问题1、没有给成员属性编写get/Set方法private UserInfo usInfo1;//问题2、返回值不是Stringprivate String usName;private String usPass;private String usSex;private Set<String> usAH;private String usJg;private Integer usAge;public String init(){System.out.println("INIT方法被调用成功");//System.out.println(usInfo1.getUsName());return "action";}public String query(){System.out.println("query方法被调用成功");//System.out.println(usInfo1.getUsName());return "OK";}/*** 默认方法*/public String execute(){System.out.println("**************************");return "OK";}public String save_init(){System.out.println("--------------------");return "errors";}public UserInfo getUsInfo() {return usInfo1;}public void setUsInfo(UserInfo usInfo1) {this.usInfo1 = usInfo1;}public String getUsName() {return usName;}public void setUsName(String usName) {this.usName = usName;}public String getUsPass() {return usPass;}public void setUsPass(String usPass) {this.usPass = usPass;}public String getUsSex() {return usSex;}public void setUsSex(String usSex) {this.usSex = usSex;}public UserInfo getUsInfo1() {return usInfo1;}public void setUsInfo1(UserInfo usInfo1) {this.usInfo1 = usInfo1;}public Integer getUsAge() {return usAge;}public void setUsAge(Integer usAge) {this.usAge = usAge;}public Set<String> getUsAH() {return usAH;}public void setUsAH(Set<String> usAH) {this.usAH = usAH;}public String getUsJg() {return usJg;}public void setUsJg(String usJg) {this.usJg = usJg;}}
三、struts.xml:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" ><struts><!-- 常量配置 --><!-- 定义字符集编码 --><constant name="struts.i18n.encoding" value="GBK"></constant>  	 <!-- 解决Post请求中文乱码	--><!-- 禁止struts2标签的默认格式生效<constant name="struts.ui.theme" value="simple"></constant>--><package name="testManager" extends="struts-default" namespace="/userManager"><!-- 请求找不到Action时 默认调用该Action --><default-action-ref name="defaultAction"></default-action-ref><!-- 全局的返回路径 --><global-results><result name="errors">/error.jsp</result></global-results><action name="testAction" class="com.zuxia.yc42.action.MyTestAction" method="init"><result name="OK">/MyJsp.jsp</result><result name="error">/index.jsp</result></action><action name="myAction" class="com.zuxia.yc42.action.MyTestAction" method="query"><result name="OK" >/MyJsp1.jsp</result><result name="error">/index.jsp</result></action><action name="myTestAction" class="com.zuxia.yc42.action.MyTestAction"><result name="OK">/MyJsp1.jsp</result><result name="error">/index.jsp</result></action><action name="*Test" class="com.zuxia.yc42.action.MyTestAction" method="{1}"><result name="OK">/MyJsp1.jsp</result><result name="error">/index.jsp</result><result name="action" type="chain"><param name="namespace">/userManager</param><param name="actionName">usManager</param><param name="method">modifyInfo</param></result></action><action name="usManager" class="com.zuxia.yc42.action.UserManagerAction" ><result name="success">/jsp/userManager/regUserInfo.jsp</result></action><action name="defaultAction"><result>/error.jsp</result></action></package><package name="testManager1" extends="struts-default" namespace="/testManager"><action name="testAction" class="com.zuxia.yc42.action.MyTestAction" method="init"><result name="OK">/MyJsp.jsp</result><result name="error">/index.jsp</result></action><action name="myAction" class="com.zuxia.yc42.action.MyTestAction" method="query"><result name="OK" >/MyJsp1.jsp</result><result name="error">/index.jsp</result></action><action name="myTestAction" class="com.zuxia.yc42.action.MyTestAction"><result name="OK">/MyJsp1.jsp</result><result name="error">/index.jsp</result></action><action name="usManager" class="com.zuxia.yc42.action.UserManagerAction" ><result name="success">/jsp/userManager/regUserInfo.jsp</result></action></package></struts>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: