第一个struts2应用程序

本文介绍了一个简单的Struts2应用实例,包括web.xml配置、Hello.java动作类及对应的JSP页面展示。通过该示例可以了解Struts2的基本工作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struts2相对于struts1来说,还是有蛮多改变的,很多东东的写法上也有了不同,大家在学习的时候就会体会的到了

web.xml

程序代码

<? xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"  xmlns=" http://java.sun.com/ xml/ns/j2ee"  xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/ xml/ns/j2ee  http://java.sun.com/ xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <filter>
        <filter-name> struts2</filter-name>
        <filter-class>org.apache. struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name> struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index. jsp</welcome-file>
    </welcome-file-list>

</web-app>



Hello.java

程序代码

package zb. struts2.test;
import com.opensymphony.xwork2.ActionSupport;
public class Hello extends ActionSupport{

    private String name;
    private String email;
    
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public String execute()
    {
        name="hello, "+name+" !";
        email="your email is: "+email+" !";
        return SUCCESS;
    }
}



Hello.jsp

程序代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/ struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>测试 struts2</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">

  </head>
  
  <body>
  <s:form action="Hello">
     <s:textfield name="name"></s:textfield>
     <s:textfield name="email"></s:textfield>
  <s:submit name="getUserInf" />
  </s:form>
  <br>
  </body>
</html>



HelloResult.jsp

程序代码

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/ struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>测试Hello的结果</title>
    </head>
    <body>
        <h3><s:property value="name" /></h3>
        <br>
        <h3><s:property value="email"/></h3>
    </body>
</html>



转载请保留出处: http://www.gold98.net/blog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值