• 第一个Struts2入门经典实例


    搭建好开发环境,现在开始进入第一个Struts入门项目。

    我用的是MyEclipse工具,首先新建一个动态Web项目HelloWorld,如图

    在MyEclipse中配置Tomcat

    从MyEclipse菜单栏window中进入preferences,在搜索框搜索“tomcat",下面出现几个tomcat,将不需要的tomcat禁用掉(Disable),只让一个你已配置好的tomcat运行(Enable),然后在tomcat home directory 设置你安装的tomcat的目录。

    在MyEclipse中启动tomcat,下面的控制台会出现Server startup的提示

     

    到官网http://struts.apache.org/download.cgi下载最新的structs2,我们需要将下载好的structs2解压,然后将其中的这些jar包复制到 WEB_INFlib 这个文件夹中

     注:(commons-lang-x.y.jar这个包需要更换为新版的commons-lang3-x.y.jar包)

     

    创建Aciton类 HelloworldAction.java

    package com.myjava.zlb.HelloworldAction;
    
    public class HelloworldAction {
        private String name;
        public String execute()throws Exception{
        	return "success";
        }
        public String getName(){
        	return name;
        }
        public void setName(){
        	this.name = name;
        }
    }
    

    创建视图(前端jsp界面)HelloWorld.jsp

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
    <title>Hello World</title>
    </head>
    <body>
       Hello World, <s:property value="name"/>
    </body>
    </html>

    创建主页面 index.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
    <%@ 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>
    <title>Hello World</title>
    </head>
    <body>
       <h1>Hello World From Struts2</h1>
       <form action="hello">
          <label for="name">Please enter your name</label><br/>
          <input type="text" name="name"/>
          <input type="submit" value="Say Hello"/>
       </form>
    </body>
    </html>

    配置struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
       "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <constant name="struts.devMode" value="true" />
       <package name="helloworld" extends="struts-default">
         
          <action name="hello" 
                class="com.myjava.zlb.HelloworldAction" 
                method="execute">
                <result name="success">/HelloWorld.jsp</result>
          </action>
       </package>
    </struts>

    配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    
    <display-name>Struts 2</display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <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>
    </web-app>

    将项目export为war包放到tomcat安装目录下的webapps文件下,此时的服务器是启动着的,会自动部属新的war包。在MyEclipse上run这个项目,会在它的浏览器上弹出index.jsp界面,如图

    输入名字zlb,点击say hello

    第一个struts2入门实例就此结束了。

  • 相关阅读:
    xutils3批量上传文件
    超详细Android面试的准备与经历分享 | 百度(offer)
    [网站公告]3月10日23:00-4:00阿里云SLB升级,会有4-8次连接闪断团队
    上周热点回顾(3.2-3.8)团队
    发布新博客皮肤red_autumnal_leaves团队
    上周热点回顾(2.23-3.1)团队
    上周热点回顾(2.16-2.22)团队
    上周热点回顾(2.2-2.8)团队
    如何禁止IE自动生成链接团队
    [云上天气预报-有时有闪电]2月3日23:00-4:00阿里云SLB升级期间网络会闪断团队
  • 原文地址:https://www.cnblogs.com/zlb2013/p/3558699.html
Copyright © 2020-2023  润新知