zoukankan      html  css  js  c++  java
  • 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中的属性名称一致,

  • 相关阅读:
    JavaScript学习总结(三)——闭包、IIFE、原型、函数与对象
    JavaScript学习总结(二)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
    CSS3与页面布局学习总结(八)——浏览器兼容与前端性能优化
    CSS3与页面布局学习总结(七)——前端预处理技术(Less、Sass、CoffeeScript、TypeScript)
    CSS3与页面布局学习总结(六)——CSS3新特性(阴影、动画、渐变、变形、伪元素等)
    CSS3与页面布局学习总结(四)——页面布局大全
    CSS3与页面布局学习总结(五)——Web Font与Sprite
    CSS3与页面布局学习总结(三)——BFC、定位、浮动、7种垂直居中方法
    CSS3与页面布局学习总结(二)——Box Model、边距折叠、内联与块标签、CSSReset
    HTML5 学习总结(四)——canvas绘图、WebGL、SVG
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6011445.html
Copyright © 2011-2022 走看看