当前位置: 首页 > news >正文

2016/10/29 action与form表单的结合使用

1>web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>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>
View Code

2>userAction.java

package com.Test.action;

public class UserAction {
    String info;

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String find() {
        if (info.endsWith(".vip")) {
            return "vip";
        }
        return "regular";
    }
}
View Code

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>
    <!-- 动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <package name="helloPackage" extends="struts-default">
        <action name="userAction" class="com.Test.action.UserAction" method="find">
            <result name="vip">/vip.jsp</result>
            <result name="regular">/nvip.jsp</result>
        </action>
    </package>
</struts>
View Code

4>index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
    <form action="userAction">
        <table border="1" width="400px" height="200px">
            <tr>
                <td>请输入用户名:</td>
                <td><input name="info" type="text" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" name="submit" value="提交" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
View Code

5>相关jsp文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>Insert title here</title>
</head>
<body>
    <h1>VIP</h1>
    <s:property value="info" />
</body>
</html>
vip.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>Insert title here</title>
</head>
<body>
    <h1>NOT VIP ,HELLOWORLD</h1>
    <s:property value="info" />
</body>
</html>
nvip.jsp

注意:1>在form表单中,action值为struts.xml中的action中的name值,习惯上在后面再写个.action,不写也没关系,

   2>在input中,name值要与action.java中的属性名称一致,

转载于:https://www.cnblogs.com/1020182600HENG/p/6011445.html

相关文章:

  • 中小企业如何搭建数据可视化平台
  • JavaScript 事件绑定及深入
  • 2016最新京东商城首页静态模板下载
  • python 日期和时间
  • Javascript的setTimeOut()和setInterval()的定时器用法
  • rsync实现同步
  • C++中static的作用和使用方法
  • 关于Html编码问题,例如字符:#183;
  • 机器学习之寻找合适的学习方法
  • 【文智背后的奥秘】系列篇——基于CRF的人名识别
  • 【转】Maven实战(九)---模块聚合和继承
  • TCP系列47—拥塞控制—10、FACK下的快速恢复与PRR
  • webpack常用加载器和插件
  • Android Studio-导入External Libraries
  • Win7 + VS2015 + CMake3.6.1-GUI + Makefile 编译开源库
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 【剑指offer】让抽象问题具体化
  • Android 架构优化~MVP 架构改造
  • Date型的使用
  • django开发-定时任务的使用
  • jdbc就是这么简单
  • leetcode讲解--894. All Possible Full Binary Trees
  • vuex 笔记整理
  • 基于游标的分页接口实现
  • 马上搞懂 GeoJSON
  • 前端技术周刊 2019-01-14:客户端存储
  • 使用parted解决大于2T的磁盘分区
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • ​草莓熊python turtle绘图代码(玫瑰花版)附源代码
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • # Panda3d 碰撞检测系统介绍
  • #android不同版本废弃api,新api。
  • #Lua:Lua调用C++生成的DLL库
  • #ubuntu# #git# repository git config --global --add safe.directory
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (强烈推荐)移动端音视频从零到上手(下)
  • (转)大型网站架构演变和知识体系
  • (转载)从 Java 代码到 Java 堆
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .net core 连接数据库,通过数据库生成Modell
  • .net反混淆脱壳工具de4dot的使用
  • .Net接口调试与案例
  • .net经典笔试题
  • // an array of int
  • ?
  • @Import注解详解
  • [C++]priority_queue的介绍及模拟实现
  • [C++参考]拷贝构造函数的参数必须是引用类型
  • [CakePHP] 在Controller中使用Helper
  • [COI2007] Sabor
  • [CSS]CSS 字体属性