第一个struts2应用程序

来源:互联网 发布:mysql 最近一年 编辑:程序博客网 时间:2024/04/30 09:59

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

原创粉丝点击