(04)Struts2_helloWorld

需求

借用前面struts2-01的需求截图

这里写图片描述

编码

这里写图片描述

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
    <a href="czy_input.action">进入Input.jsp页面</a>
</body>
</html>

Input.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <form action="czy_save.action" method="post">
       编号:<input type="text" name="dlh"/><br>
       姓名:<input type="text" name="name"/><br>
       部门:<input type="text" name="bmmc"/><br>
       <input type="submit" value="保存"/>
   </form>
  </body>
</html>

Show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
       ID:${id}<br>
       编号:${ dlh}<br>
       姓名:${ name}<br>
       部门:${ bmmc}<br>
  </body>
</html>

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>struts2-02</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 配置Struts2的过滤器 -->
    <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>

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: 包. struts2 使用 package 来组织模块. 
        name 属性: 必须. 用于其它的包应用当前包. 
        extends: 当前包继承哪个包, 继承的, 即可以继承其中的所有的配置. 通常情况下继承 struts-default
                 struts-default 这个包在 struts-default.xml 文件中定义.
        namespace 可选, 如果它没有给出, 则以 / 为默认值. 
                                若 namespace 有一个非默认值, 则要想调用这个包里的Action, 
                                就必须把这个属性所定义的命名空间添加到有关的 URI 字符串里

                  http://localhost:8080/contextPath/namespace/actionName.action
    -->
    <package name="helloworld" extends="struts-default" namespace="/">
        <!-- 
            配置一个 action: 一个 struts2 的请求就是一个 action 
            name: 对应一个 struts2 的请求的名字(或对一个 servletPath, 但去除 / 和扩展名), 不包含扩展名
            class 的默认值为: com.opensymphony.xwork2.ActionSupport
            method 的默认值为: execute
            result: 结果. 
        -->
        <action name="czy_input" >
            <!--  
                result: 结果. 表示 action 方法执行后可能返回的一个结果. 所以一个 action 节点可能会有多个 result 子节点.
                多个 result 子节点使用 name 来区分
                name: 标识一个 result. 和 action 方法的返回值对应. 默认值为 success
                type: 表示结果的类型. 默认值为 dispatcher(转发到结果.)
            -->
            <result >/WEB-INF/page/Input.jsp</result>
        </action>
        <action name="czy_save" class="com.qbz.struts2_02.GG_CZY" method="save">
            <result name="ok">/WEB-INF/page/Show.jsp</result>
        </action>
    </package>
</struts>

GG_CYZ.java

package com.qbz.struts2_02;

import java.util.UUID;


public class GG_CZY {
    private String id;
    private String dlh;
    private String name;
    private String bmmc;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDlh() {
        return dlh;
    }

    public void setDlh(String dlh) {
        this.dlh = dlh;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBmmc() {
        return bmmc;
    }

    public void setBmmc(String bmmc) {
        this.bmmc = bmmc;
    }

    public GG_CZY(String id, String dlh, String name, String bmmc) {
        super();
        this.id = id;
        this.dlh = dlh;
        this.name = name;
        this.bmmc = bmmc;
    }

    public GG_CZY() {
        super();
    }

    @Override
    public String toString() {
        return "GG_CZY [id=" + id + ", dlh=" + dlh + ", name=" + name
                + ", bmmc=" + bmmc + "]";
    }

    public String save() {
        if(this.id==null){
            this.id = UUID.randomUUID().toString().replace("-", "");
        }
        return "ok";
    }
}
至此,使用struts2实现了最简单的网页跳转、后台业务处理等helloWorld功能。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值