zoukankan      html  css  js  c++  java
  • 第一个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入门实例就此结束了。

  • 相关阅读:
    用Photoshop设计网站的70个教程
    反射引发的错误“reflection Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.”
    机械键盘理解的几个误区和保养方法
    Asp.Net MVC中不指定View Name时如何返回ActionResult的
    [通告]Nuget服务宕机,出现 503 Server Unavailable 错误无法编译及解决方法
    MVC标签链接切换帮助类: TabLink
    [Centos 6.2] centos 6.2(64位)网络配置
    Linux(Ubuntu)设置环境变量(转载)
    COPYONWRITE 原理
    [Linux 技巧] linux screen 命令详解
  • 原文地址:https://www.cnblogs.com/zlb2013/p/3558699.html
Copyright © 2011-2022 走看看