web10 动态action的应用

电影网站:www.aikan66.com 

项目网站:www.aikan66.com 
游戏网站:www.aikan66.com 
图片网站:www.aikan66.com 
书籍网站:www.aikan66.com 
学习网站:www.aikan66.com 
Java网站:www.aikan66.com 
iOS网站:www.aikan66.com

----

用Struts提供的动态action,处理添加用户信息请求及更新用户信息请求

----

1、创建web项目,jwrm03-dongtaiAction,拷贝Struts包到lib,web.xml中注册Struts2的过滤器。(详见web08)

----

2、创建UserAction的Action对象,并在这个Action对象中编写add(),update()

package com.aikan66.action;
import com.opensymphony.xwork2.ActionSupport;

/*
 * 用户action
 * @auther cpy
 */
public class UserAction extends ActionSupport{
    private static final long serialVersionUID=1L;
    //提示信息
    private String info;
    
    //添加用户信息
    public String add() throws Exception{
        info="添加用户信息";
        return "add";
    }
    
    //更新用户信息
    public String update() throws Exception{
        info="更新用户信息";
        return "update";
    }
    
    public String getInfo(){
        return info;
    }
    
    public void setInfo(String info){
        this.info=info;
    }
}

----

3、配置struts.xml。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 声明包 -->
    <package name="myPackage" extends="struts-default">
        <!-- 定义action -->
        <action name="userAction" class="com.aikan66.action.UserAction">
            <!-- 添加信息成功后的映射页面 -->
            <result name="add">user_add.jsp</result>
            <!-- 更新信息成功后的映射页面 -->
            <result name="update">user_update.jsp</result>
        </action>
    </package> 
</struts>

----

4、创建user_add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_add.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>
    <font color="red">
        <s:property value="info"/>
    </font>
  </body>
</html>

类似创建user_update.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_update.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>
    <font color="red">
        <s:property value="info"/>
    </font>
  </body>
</html>

index.jsp中添加超级链接

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>  
<!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="userAction!add">添加用户信息</a><p>
    <a href="userAction!update">更新用户信息</a><br>
  </body>
</html>

----

5、部署,访问http://localhost:8080/jwrm03-dongtaiAction/index.jsp

点击“添加用户信息”

点击“更新用户信息”

----

完毕

posted @ 2016-07-28 20:14  摘丶星  阅读(379)  评论(0编辑  收藏  举报
我要啦免费统计