web10 動態action的應用

電影網站:www.aikan66.com css

項目網站:www.aikan66.com 
遊戲網站:www.aikan66.com 
圖片網站:www.aikan66.com 
書籍網站:www.aikan66.com 
學習網站:www.aikan66.com 
Java網站:www.aikan66.com 
iOS網站:www.aikan66.comhtml

----java

用Struts提供的動態action,處理添加用戶信息請求及更新用戶信息請求web

----apache

一、建立web項目,jwrm03-dongtaiAction,拷貝Struts包到lib,web.xml中註冊Struts2的過濾器。(詳見web08)jsp

----學習

二、建立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;
    }
}

----ui

三、配置struts.xml。this

<?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>

----

四、建立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>

----

五、部署,訪問http://localhost:8080/jwrm03-dongtaiAction/index.jsp

點擊「添加用戶信息」

點擊「更新用戶信息」

----

完畢

相關文章
相關標籤/搜索