struts2的tags显示后台返回的数据

在struts2开发过程中,最基本的要使用前端传入数据到后台,后台返回数据要在前端显示。

有两种方式可以触发后台功能

 a) 访问功能页面,由页面form触发action  http://localhost:8080/struts_test/pages/login.jsp

 b) 直接访问.do :   http://localhost:8080/struts_test/pages/Login.do

代码结构:


示例代码如下:

1. struts配置(struts.xml,web.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.action.extension" value="do" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!--  
    <include file="mailreader-default.xml"/>

    <include file="mailreader-support.xml"/>
-->
	<package name="default" namespace="/" extends="struts-default">  
        <action name="Login" class="action.helloAction">  
            <result name="error">/pages/error.jsp</result>  
            <result name="success">/pages/success.jsp</result>  
        </action>  
    </package>  
</struts>


<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>struts_test</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	
	<filter>
        <filter-name>Struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
	
</web-app>


 



2. 前端数据数据页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>struts test page</title>
</head>
<body>
<form action="/struts_test/Login.do" method="post">  
  
        <table align="left" width="50%" border="0" cellpadding="0"  
            cellspacing="0">  
  
            <tr>  
                <td></td>  
            </tr>  
            <tr>  
  
                <td height="30" bgcolor="" align="center" valign="middle">用户名:</td>  
                <td align="left" height="30" valign="middle"><input  
                    style="width: 150px" type="text" name="username" value=""></input></td>  
  
            </tr>  
  
  
            <tr>  
                <td height="30" bgcolor="" align="center" valign="middle">密 码:</td>  
                <td align="left" height="30" valign="middle"><input  
                    style="width: 150px" type="password" name="password" value=""></input></td>  
            </tr>  
  
            <tr>  
                <td align="right"><input type="submit" name="button" value="提交"></input></td>  
            </tr>  
        </table>  
    </form> 
</body>
</html>

3. 后台处理的JAVA程序

package action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport; 

public class helloAction extends  ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;  
    private String password;  

  
    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;  
    }  
    private List<Map<String,String>>  contentList ;
    private List<Map<String,String>> header;
    public List<Map<String,String>> getContentList() {
         return contentList;
    }

    public void setContentList(List<Map<String,String>> contentList) {
         this.contentList = contentList;
    }
    
    

	public List<Map<String, String>> getHeader() {
		return header;
	}

	public void setHeader(List<Map<String, String>> header) {
		this.header = header;
	}

	public String execute() throws Exception {  
        if (null != username && username.equalsIgnoreCase("111")) {
            List<Map<String,String>> l = new ArrayList<Map<String,String>>();
            for(int i=0;i<10;i++){
            	Map<String,String> m = new HashMap<String, String>();
            	m.put("name", username+"_"+i);
            	m.put("sex", i+"");
            	m.put("age", i+10+"");
            	l.add(m);
            	}
            this.setContentList(l);
            return "success";  
        }  
        
        List<Map<String,String>> l = new ArrayList<Map<String,String>>();
        for(int i=0;i<10;i++){
        	Map<String,String> m = new HashMap<String, String>();
        	m.put("name", "张三"+"_"+i);
        	m.put("sex", i+"");
        	m.put("age", i+10+"");
        	l.add(m);
        	}
        this.setContentList(l);
        List<Map<String,String>> f = new ArrayList<Map<String,String>>();
        Map<String,String> h = new HashMap<String, String>();
        h.put("addr", "深圳");
        h.put("zipcode", "518000");
        f.add(h);
        this.setHeader(f);
        return "error";
        
  
    }  
}

4. 前台展示后台数据页面(error.jsp、success.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="GB18030"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>struts test page</title>
</head>
<body>
<center>Login Success!!!</center>  
<table border=1>
	<tr>
		<td>name</td>
		<td>sex</td>
		<td>age</td>
	</tr>
	<s:iterator value="contentList" id="rh" status="status">
	<tr>
		<td><s:property value="name"/> </td>
		<td><s:property value="sex"/> </td>
		<td><s:property value="age"/> </td>
	</tr>
	</s:iterator>
</table>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="GB18030"%>
 <%@ taglib uri="/struts-tags" prefix="s" %>
<!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>struts test page</title>
</head>
<body>
<center>login error!!!</center>
<table border=1>
	<tr>
		<s:iterator value="header" id="column">
		<td><s:property value="addr"/></td>
		<td><s:property value="zipcode"/></td>
		</s:iterator>
	</tr>
</table>
<table border=1>
	<tr>
		<td>name</td>
		<td>sex</td>
		<td>age</td>
	</tr>
	<s:iterator value="contentList" id="rh" status="status">
	<tr>
		<td><s:property value="name"/> </td>
		<td><s:property value="sex"/> </td>
		<td><s:property value="age"/> </td>
	</tr>
	</s:iterator>
</table>
</body>
</html>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值