zoukankan      html  css  js  c++  java
  • [Struts2学习笔记] -- 环境配置

      在创建好WebProject后,就可以开始进行Struts2的环境配置,可以到Struts2官网下载,本环境使用struts-2.3.24.1版本。

      首先导入必要的jar包到WebProject的/WebRoot/WEB-INF/lib下,具体jar包如下图所示:

      

      接着修改web.xml文件,加入struts2的配置信息,文件内容如下:

    <?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>myStruts2</display-name>
      <!-- struts2 configuration -->
      <filter>
          <filter-name>struts2</filter-name>
          <filter-class>
              org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
          </filter-class>
          <init-param>
            <param-name>actionPackages</param-name>
            <param-value>cn.net.bysoft</param-value>
        </init-param>
      </filter>
      <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>
    web.xml的配置

      现在struts2的环境配置已经建立完毕,可以编写一个HelloWorld应用进行测试了,首先创建两个jsp页面,一个是index.jsp,里面写一个<a href="helloWorld">测试HelloWorld应用</a>连接到Action。接着创建一个hello.jsp页面用来显示<h1>Hello World</h1>,具体代码如下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</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">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <a href="helloWorld">test helloworld</a>
      </body>
    </html>
    index.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'hello.jsp' starting page</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">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        HelloWorld <br>
      </body>
    </html>
    hello.jsp

      页面创建完毕后,开始编写Action类,创建一个普通的类,继承ActionSupport类。编写execute()方法进行页面控制,返回一个成功的标识SUCCESS,具体代码如下:

    package cn.net.bysoft;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloWorldAction extends ActionSupport {
    
        /**
         * 
         */
        private static final long serialVersionUID = 6649419922238488318L;
    
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            return SUCCESS;
        }
        
    }
    HelloWorldAction的代码

      最后在/src目录下创建一个struts.xml文件,对我们编写的Action进行配置,文件内容如下:

    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <package name="struts2_3_24_1" extends="struts-default">
            <action name="helloWorld" class="cn.net.bysoft.HelloWorldAction">
                <result>/hello.jsp</result>
            </action>
        </package>
    </struts>
    struts.xml的内容

      以下是项目结构:

  • 相关阅读:
    开源项目之Android StandOut(浮动窗口)
    小智慧7
    安卓学习
    asp.net学习Request和Response的其他属性
    bash中的转义
    POJ 1833 排列
    Django点滴(四)ORM对象存取
    POJ 1681 Painter's Problem
    linux2.6.32在mini2440开发板上移植(21)之WebServer服务器移植
    [gkk传智]static与多态及向下向上转型,及多态调用总结
  • 原文地址:https://www.cnblogs.com/DeadGardens/p/5140741.html
Copyright © 2011-2022 走看看