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

java 从零开始,学习笔记之基础入门<Struts2_实战演练(下)>(四十)

2014-02-18 12:41 956 查看
 Struts2_实战演练(下)
国际化
1.        导入相关的包
2.        配置国际化资源文件:gobalMessage_en_US.properties(src目录下,1.前面部分保持一致)
中英文切换部分
Index.jsp
<%@
page language="java"
import="java.util.*"
pageEncoding="UTF-8"%>
<%@taglib
prefix="s"
uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE
HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base
href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</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">
    -->
  </head>
 
  <body>
    //核心部分
    <a
href="i18n?request_locale=zh_CN">简体中文</a><br/>
    <a
href="i18n?request_locale=en_US">English</a>
    <hr/>
   
    <form
action="">
       <table
align="center"
border="2"
bgcolor="#899ff9">
           <tr>
              <td
align="center"><s:text
name="key1"></s:text></td>
           </tr>
           <tr>
              <td><s:text
name="key2"></s:text></td>
              <td><input
type="text"
name="username"/></td>
           </tr>
           <tr>
              <td><s:text
name="key3"></s:text></td>
              <td><input
type="password"
name="pwd"/></td>
           </tr>
           <tr>
              <td><input
type="submit"
value="<s:text
name="key4"></s:text>"/></td>
           </tr>
       </table>
    </form>
 
  </body>
</html>
 
 
Struts.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>
 
    <constant
name="struts.custom.i18n.resources"
value="gobalMessage"></constant>
   
    <package
name="struts"
extends="struts-default">
       <action
name="i18n"
class="com.ibm.i18n.util.I18NDemo">
           <result
name="success">index.jsp</result>
       </action>
    </package>
</struts>
 
I18Ndemo.java(只是实现过度作用)
package com.ibm.i18n.util;
 
import com.opensymphony.xwork2.ActionSupport;
 
public
class
I18NDemo extends ActionSupport {
    @Override
    public String execute()
throws Exception {
       return
"success";
    }
 
}
 
两个配置文件(中文和英文)
gobalMessage_en_US.properties
key1=Welcome!
key2=Username:
key3=Userpwd:
key4=Login
 
gobalMessage_zh_CN.properties
key1=\u6b22\u8fce\u767b\u9646!
key2=\u7528\u6237\u540d:
key3=\u5bc6\u7801:
key4=\u767b\u5f55
 
 
 
文件上传
employerreg.jsp
<%@
page language="java"
import="java.util.*"
pageEncoding="UTF-8"%>
<%@taglib
uri="/struts-tags"
prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE
HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base
href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</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">
    -->
  </head>
 
  <body>
    员工信息注册
    <hr/>
    <s:form
action="empregaction.do"
name=""
id="empform"
method="post"
enctype="multipart/form-data">
        <s:textfield  
label="员工姓名"
required="true"
name="uname"
id="uname"></s:textfield>
        <br/>
       
        <s:select
label="员工年龄"
required="true"
list="{'20','21','22','23','24'}"
headerKey="-1"

                 name="age"
headerValue="请选择年龄"
multiple="false">
        </s:select>
        <br/>
       
        <s:radio
label="员工性别 "
list="{'男','女'}"
name="sex"></s:radio>
        <br/>
       
        <s:file
label="员工头像1"
required="true"
name="headimg"></s:file>
        <s:file
label="员工头像2"
required="true"
name="headimg"></s:file>
        <s:file
label="员工头像3"
required="true"
name="headimg"></s:file>
       
        <s:submit
value="员工信息注册"></s:submit>
       
    </s:form>
  </body>
</html>
 
上传中配置及拦截器部分
Struts.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>
   
    <constant
name="struts.custom.i18n.resources"
value="globalmessage"></constant>
 
    <package
name="userpackage"
extends="struts-default">
       <action
name="register"
class="com.ibm.action.UserOperAction">
           <result
name="success">success.jsp</result>
           <result
name="input">index.jsp</result>
       </action>
 
       <action
name="empregaction"
class="com.ibm.action.EmployerOperAction">
                <interceptor-ref
name="fileUpload">
                      <param
name ="allowedTypes">
                      image/bmp,image/x-png,image/gif,image/pjpeg,image/jpg
                      </param>
                       <param
name="maximumSize">50000</param>
                </interceptor-ref>
             <interceptor-ref
name="defaultStack"></interceptor-ref>     

           <result
name="success">success.jsp</result>
           <result
name="input">employerreg.jsp</result>
       </action>
    </package>
</struts>
属性文件中的配置及注解
 
      Struts2提供文件上传支持
      Struts2的配置文件中配置
      #指定使用COS的文件上传解析器
      #struts.multipart.parser=cos
      #指定使用Pell的文件上传解析器
      #struts.multipart.parser=pell
      #Struts2默认使用Jakarta的Common-FileUpload的上传解析器
      struts.multipart.parset=jakarta
 
Struts.xml
struts.action.extension=do
 
struts.multipart.parset=jakarta
 
出错了以下属性文件部分会给出提示
globalmessage.properties(提示信息)
#????????????????
struts.messages.error.content.type.not.allowed=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\:\u4F60\u8981\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u4E0D\u5141\u8BB8
#?????????????
struts.messages.error.file.too.large=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\:\u4F60\u8981\u4E0A\u4F20\u7684\u6587\u4EF6\u592A\u5927
#??????????
struts.messages.error.uploading=\u6587\u4EF6\u4E0A\u4F20\u5931\u8D25\:\u53D1\u751F\u5185\u90E8\u9519\u8BEF

 










文件上传核心部分
EmployerOperAction.java
package com.ibm.action;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
 
import javax.servlet.ServletContext;
 
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionSupport;
 
public
class
EmployerOperAction extends ActionSupport{
   
    private String
uname;
   
    private Integer
age;
   
    private String
sex;
   
   
    /**
     * 此部分针对单文件上传
     * @return
     */
//  //headimg和表单中的filename是一一匹配的
//  private File headimg;
//  //headimgFileName中的FileName是固定的写法
//  private String headimgFileName;
文件域有文件名和文件,所以有两个属性
    private File
headimg[];
   
    private String
headimgFileName[];
   
    public File[] getHeadimg() {
       return
headimg;
    }
 
    public
void
setHeadimg(File[] headimg) {
       this.headimg = headimg;
    }
 
    public String[] getHeadimgFileName() {
       return
headimgFileName;
    }
 
    public
void
setHeadimgFileName(String[] headimgFileName) {
       this.headimgFileName = headimgFileName;
    }
 
    public String getUname() {
       return
uname;
    }
 
    public
void
setUname(String uname) {
       this.uname = uname;
    }
 
    public Integer getAge() {
       return
age;
    }
 
    public
void
setAge(Integer age) {
       this.age = age;
    }
 
    public String getSex() {
       return
sex;
    }
 
    public
void
setSex(String sex) {
       this.sex = sex;
    }
 
   
//  public File getHeadimg() {
//     return headimg;
//  }
//
//  public void setHeadimg(File headimg) {
//     this.headimg = headimg;
//  }
//
//  public String getHeadimgFileName() {
//     return headimgFileName;
//  }
//
//  public void setHeadimgFileName(String headimgFileName) {
//     this.headimgFileName = headimgFileName;
//  }
 
    @Override
    public String execute()
throws Exception {
       /**
        * 此部分针对单文件的上传操作
        */
//     System.out.println("头像名是:"+headimgFileName);
//    
       ServletContext context = ServletActionContext.getServletContext();
       String picPath=context.getRealPath("pic");
       System.out.println("服务器上传图片的目录是:"+picPath);
//    
//     InputStream is = new FileInputStream(getHeadimg());
//    
//     byte[] buffer = new byte[1024];
//    
//     int len=0;
//     OutputStream os = new FileOutputStream(picPath+File.separator+getHeadimgFileName());
//    
//     while((len=is.read(buffer))!=-1){
//         os.write(buffer, 0, len);
//     }
//     os.flush();
//     os.close();
//     is.close();
      
      
       /***************多文件上传*************/
       InputStream is=null;
       OutputStream os=null;
       for (int i=0;i<headimg.length;i++) {
           System.out.println(headimg[i]);
           is = new FileInputStream(getHeadimg()[i]);
          
           byte[] buffer =
new byte[1024];
          
           int len=0;
           os = new FileOutputStream(picPath+File.separator+getHeadimgFileName()[i]);
          
           while((len=is.read(buffer))!=-1){
              os.write(buffer, 0, len);
           }
       }
      
       os.flush();
       os.close();
       is.close();
      
       return
"success";
      
    }
 
}
 
 
文件下载部分
Download.jsp
<%@
page language="java"
import="java.util.*"
pageEncoding="UTF-8"%>
<%@taglib
prefix="s"
uri="/struts-tags"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE
HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base
href="<%=basePath%>">
   
    <title>My JSP 'download.jsp' starting page</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">
    -->
 
  </head>
 
  <body>
     文件下载演示:
    <hr/>
    <s:url
action="download.do"
var="download"
id="download">
        <s:param
name="fileName">1.zip</s:param>
    </s:url>
   
    <s:a
href="%{download}">下载文件</s:a>
   
  </body>
</html>
 
 
Struts.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>
   
    <constant
name="struts.custom.i18n.resources"
value="globalmessage"></constant>
 
    <package
name="userpackage"
extends="struts-default">
       <action
name="register"
class="com.ibm.action.UserOperAction">
           <result
name="success">success.jsp</result>
           <result
name="input">index.jsp</result>
       </action>
 
       <action
name="empregaction"
class="com.ibm.action.EmployerOperAction">
                <interceptor-ref
name="fileUpload">
                      <param
name ="allowedTypes">
                      image/bmp,image/x-png,image/gif,image/pjpeg,image/jpg
                      </param>
                       <param
name="maximumSize">50</param>
                </interceptor-ref>
             <interceptor-ref
name="defaultStack"></interceptor-ref>     

           <result
name="success">success.jsp</result>
           <result
name="input">employerreg.jsp</result>
       </action>
      
       <action
name="download"
class="com.ibm.action.DownloadAction">
           <param
name="downLoadPath">pic</param>
          
           <result
type="stream"
name="success">
              <!-- 文件类型 -->
              <param
name="contentType">application/zip</param>
              <!-- 传递到客户端的文件名 -->
              <param
name="contentDisposition">
                  fileName=${fileName}
              </param>
           </result>
       </action>
 
       <action
name="getUserList"
class="com.ibm.action.UserOperAction"
method="getUserList">
           <result
name="success">userlist.jsp</result>
       </action>
      
      
    </package>
</struts>
下载核心部分
DownloadAction.java
package com.ibm.action;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
 
import javax.servlet.ServletContext;
 
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionSupport;
 
public
class
DownloadAction extends ActionSupport{
   
    //下载的文件名称
    private String
fileName;
    //下载的文件路径
    private String
downLoadPath;
    //文件输入流对象
    private InputStream
inputStream;
   
    @Override
    public String execute()
throws Exception {
       System.out.println("下载的文件名是:"+fileName);
       return
"success";
    }
   
   
    public String getFileName() {
       System.out.println("客户端请求服务器端传递数据");
       return
fileName;
    }
    public
void
setFileName(String fileName) {
       System.out.println("数据从客户端传递来");
       this.fileName = fileName;
    }
    public String getDownLoadPath() {
       return
downLoadPath;
    }
    public
void
setDownLoadPath(String downLoadPath) {
       this.downLoadPath = downLoadPath;
    }
   
    /**
     * 为inputstream对象赋值
     * @return inputStream
     */
    public InputStream getInputStream() {
       //获得当前action的上下文环境
       ServletContext context = ServletActionContext.getServletContext();
       //获得pic目录在服务器中的地址
       String realPath=context.getRealPath("pic");
       System.out.println("pic所在地址:"+realPath);
       try {
           inputStream =
new
FileInputStream(realPath+File.separator+getFileName());
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       return
inputStream;
    }
    public
void
setInputStream(InputStream inputStream) {
       this.inputStream = inputStream;
    }  
}
 
Ognl语言的使用
页面显示
index.jsp
<%@ page
language="java"
import="java.util.*"
pageEncoding="UTF-8"%>

<%@taglib
prefix="s"
uri="/struts-tags"
%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base
href="<%=basePath%>">

   

    <title>My JSP 'index.jsp' starting page</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">

    -->

  </head>

 

  <body>

  <s:debug/>

    <form
action="UserOperAction.do"
name="formtest"
id="formtest">

       <input
type="text"
name="user.id"
id="userid"/>

       <input
type="text"
name="user.username"
id="username"/>

       <input
type="text"
name="user.userpass"
id="password"/>

       <input
type="submit"
value="登录">

       <hr>

       <br/>

       --对象属性,普通属性及方法的访问:

       <br/>

       <s:property
value="user.id"/><br/>

       <s:property
value="user.username"/><br>

       <s:property
value="user.userpass"/><br>

       <s:property
value="user.print()"/><br>

       <s:property
value="msg"/><br>

       <hr>

       <br>

       --map集合的取值

       <br>

       <s:property
value="map.keys"/><br>

       <s:property
value="map.values"/><br>

       <s:property
value="map.zhangsan.username"/><br>

       <s:property
value="map.lisi.username"/><br>

 

       <s:iterator
value="map.keys"
var="key">

           <s:property
value="key"/>

       </s:iterator><br>

       <hr/>

       --list集合的取值

       <br>

       <s:property
value="list"/><br>

       <s:property
value="list[1].username"/><br>

       <s:property
value="list.{username}[0]"/><br>

       <hr/>

           --list集合取值的筛选(this表示前面的那个属性)

       <s:iterator
value="list"
var="list">

           <s:property
value="#list.username.{?#this=='qq'}[0]"/>--

       </s:iterator>

       <br>

       <hr/>

      

    </form>

  </body>

</html>

User.java
package com.ibm.dto;

 

public
class
User {

    private Integer
id;

   

    private String
username;

   

    private String
userpass;

 

    public Integer getId() {

       return
id;

    }

 

    public
void
setId(Integer id) {

       this.id = id;

    }

 

    public String getUsername() {

       return
username;

    }

 

    public
void
setUsername(String username) {

       this.username = username;

    }

 

    public String getUserpass() {

       return
userpass;

    }

 

    public
void
setUserpass(String userpass) {

       this.userpass = userpass;

    }

   

    public String print(){

       System.out.println("测试方法");

       System.out.println("username:"+getUsername()+getUserpass());

       return
"测试成功";

    }

}

UserOperAction.java
package com.ibm.action;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import com.opensymphony.xwork2.ActionSupport;

import com.ibm.dto.User;

 

public
class
UserOperAction extends ActionSupport{

   

    User user;

   

    private String
msg="userage";

   

    private Map<String,User>
map;

   

    private List<User>
list = new ArrayList<User>();

   

    public List<User> getList() {

       return
list;

    }

 

    public
void
setList(List<User> list) {

       this.list = list;

    }

 

    public String execute()
throws Exception {

       map = new HashMap<String, User>();

       User user1 = new User();

       User user2 = new User();

       user2.setId(2);

       user2.setUsername("zhangsan");

       map.put("zhangsan",user);

       map.put("lisi",user2);

      

       list.add(user);

       list.add(user2);

      

       System.out.println(user.getUsername());

       return
"success";

    }

      

    public User getUser() {

       return
user;

    }

 

    public
void
setUser(User user) {

       this.user = user;

    }

 

    public
void
setMsg(String msg) {

       this.msg = msg;

    }

 

    public String getMsg() {

       return
msg;

    }

 

    public Map<String, User> getMap() {

       return
map;

    }

 

    public
void
setMap(Map<String, User> map) {

       this.map = map;

    }

}

 
 
 
 
Struts标签
Tagdemo.jsp
<%@
page language="java"
import="java.util.*"
pageEncoding="UTF-8"%>
<%@taglib
prefix="s"
uri="/struts-tags"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE
HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base
href="<%=basePath%>">
   
    <title>My JSP 'tagldemo.jsp' starting page</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">
    -->
 
  </head>
 
  <body>
    <hr>
    <s:debug></s:debug>
   
    <!-- 声明一个变量age -->
    <s:set
value="10"
name="age"></s:set>
   
    <s:if
test="#age<18">
    少年
    </s:if>
    <s:elseif
test="18<#age<30">
    青年
    </s:elseif>
    <s:else>
    中年/老年
    </s:else>
   
    <hr/>
   
    append标签 主要用于将集合元素合并成一个
    <br/>
    <s:append
id = "booklist">
    <s:param
value="{'java编程思想','java编程思想','Oracle'}"></s:param>
    <s:param
value="{'web编程宝典','Struts in action'}"></s:param>
    </s:append>
   
    <s:iterator
value="#booklist">
    书名是:<s:property/>
    <br/>
    </s:iterator>
   
    <hr/>
   
    genarator 标签类似split函数的作用<br>
    <s:generator
separator=","
val="'java核心编程思想,Think in java,web编程宝典'"
var="bookstr"
count="3">
   </s:generator>
  
   <s:iterator
value="#bookstr">
        <s:property/>
        <br>
   </s:iterator>
  
    <hr>
            子结果集  start=0 表示从第一个开始 count=3取出的最大个数
                <br/>
    <s:subset
source="{'胡锦涛','江泽民','邓小平'}"
start="0"
count="3"
id="sublist">
    <s:iterator
status="status">
        人名是:<s:property/>
        <br/>
    </s:iterator>
    </s:subset>
   
   <hr/>
   bean标签的演示<br>
   <s:bean
name="com.ibm.dto.User"
var="user">
        <s:param
name="uname">james gosling</s:param>
        <s:param
name="age">60</s:param>
       
        <s:property
value="age"/>
   </s:bean>
   
    用户名是:<s:property
value="#user.uname"/>
    <s:debug/>
  </body>
</html>
                    
用户信息列表展示模块
nav.jsp
<%@
page language="java"
import="java.util.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE
HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base
href="<%=basePath%>">
   
    <title>导航页面</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">
    -->
 
  </head>
 
  <body>
    <a
href="getUserList.do">访问用户信息列表</a>
  </body>
</html>
 
 
Struts.xml(action部分)
       <action
name="getUserList"
class="com.ibm.action.UserOperAction"
method="getUserList">
           <result
name="success">userlist.jsp</result>
       </action>
建立list集合
UserOperAction.java
package com.ibm.action;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import org.apache.struts2.interceptor.validation.SkipValidation;
 
import com.opensymphony.xwork2.ActionSupport;
import com.ibm.dto.User;
 
public
class
UserOperAction extends ActionSupport {
    private String
uname;
 
    private Integer
age;
 
    private Date
birthday;
 
    private List<User>
list;
 
    public List<User> getList() {
       return
list;
    }
   
    public
void
setList(List<User> list) {
       this.list = list;
    }
   
    public String getUname() {
       return
uname;
    }
 
    public
void
setUname(String uname) {
       this.uname = uname;
    }
 
    public Integer getAge() {
       return
age;
    }
 
    public
void
setAge(Integer age) {
       this.age = age;
    }
 
    public Date getBirthday() {
       return
birthday;
    }
 
    public
void
setBirthday(Date birthday) {
       this.birthday = birthday;
    }
 
    // 系统缺省不予验证的方法(跳过验证)
    // @SkipValidation
    public String execute()
throws Exception {
       System.out.println("转换后的日期:" +
birthday);
       return
"success";
    }
 
    // 此方法用来校验用户输入的信息,手动校验
    // @Override
    // public void validate() {
    // // super.validate();
    // if((uname)!=null&&uname.length()>4){
    // addFieldError("msg","输出的内容超出了制定的范围,请重新输入");
    // }
    //    
    // }
 
    /**
     * 获得用户信息表
     */
    @SkipValidation
    public String getUserList() {
    //伪代码部分
       list = new ArrayList<User>();
       for (int i = 15; i < 30; i++) {
           User u = new User();
           u.setUname("古永锵" + i);
           u.setAge(i);
           list.add(u);
       }
       return
"success";
    }
 
    public String checkAge(int age){
       if(age<19){
           return
"少年";
       }else{
           return
"青年1";
       }
    }
   
}
 
页面显示
userlist.jsp
<%@
page language="java"
import="java.util.*"
pageEncoding="UTF-8"%>
<%@taglib
prefix="s"
uri="/struts-tags"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE
HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base
href="<%=basePath%>">
   
    <title>My JSP 'userlist.jsp' starting page</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">
    -->
 
  </head>
 
  <body>
  <table
align="left"
bgcolor="orange"
border="1px;">
    <tr
align="center">
        <td>编号</td>
        <td>姓名</td>
        <td>年龄段</td>
        <td>操作</td>
    </tr>
 
    <s:iterator
value="list"
status="st">
    <s:if
test="%{#st.even==true}">
    <tr
align="center"
bgcolor="gray">
        <td><s:property
value="#st.index"/></td>
        <td><s:property
value="uname"/></td>
       
        <td>
            <s:if
test="age<19">
               少年
            </s:if>
            <s:else>
               青年
            </s:else>
        </td>
 
        <td><a
href="addUser.do">增加用户</a></td>
    </tr>
    </s:if>
   
    <s:else>
    <tr
align="center"
bgcolor="#ffaaaa">
        <td><s:property
value="#st.index"/></td>
        <td><s:property
value="uname"/></td>
        <td>
        <s:property
value="checkAge(age)"/>
        </td>
        <td><a
href="addUser.do">增加用户</a></td>
    </tr>
    </s:else>
    </s:iterator>

  </table>
  </body>
</html>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐