zoukankan      html  css  js  c++  java
  • Struts2 实战经验 之 入门

    Struts2以WebWork为核心,采用拦截器机制处理用户请求,这样的设计使业务逻辑控制器能够与Servlet API完全脱离开,降低了藕联性。

    Part 1. 安装与配置

     下载struts-2.3.15.1-all.zip,这里采用该版本,打开后有文件夹:apps 实例代码 ,docs 帮助文档,lib jar包,src 源代码文件;

    Part 2.开始写项目

    1.添加jar包;(可直接复制实例项目,复制jar)

    2.在web.xml中加入struts2的 filter ,内容如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app id="WebApp_9",version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     3 xmlns:xsi="http://www/w3/org/2001/XMLSchema-instance"
     4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
     5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     6      <filter>
     7           <filter-name>struts2</filter-name>
     8           <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     9      </filter>
    10 
    11      <filter-mapping>
    12            <filter-name>struts2</filter-name>
    13            <url-pattern>/*</url-pattern>
    14      </filter-mapping>
    15 
    16      <welcome-file-list>
    17             <welcome-file>index.html<welcome-file>
    18      </welcome-file-list>
    19 </web-app>

    从配置文件中,可以看出, Struts2 其实就是一个filter

    3.在src文件夹下添加struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
          "http://struts.apache.org/dtds/struts-2.3.dtd">
    
            <struts>
                   <package name="default" namespace="/" extends="struts-default">
                            <action name="index">
                                      <result>/index.jsp</result>
                            </action>
                   </package>
            </struts>

    4. 写jsp页面,或html页面;写java bean, action, dbutils;

    最简单的示例,在web文件夹下写 index.jsp

    <%@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>
       </head>
        <body>
                First One
         </body>
    </html>

    测试:部署tomcat或直接使用preview server,http://localhost:port/项目名/index.adtion

    注意, .action 

  • 相关阅读:
    107. Binary Tree Level Order Traversal II
    108. Convert Sorted Array to Binary Search Tree
    111. Minimum Depth of Binary Tree
    49. Group Anagrams
    使用MALTAB标定实践记录
    442. Find All Duplicates in an Array
    522. Longest Uncommon Subsequence II
    354. Russian Doll Envelopes
    opencv 小任务3 灰度直方图
    opencv 小任务2 灰度
  • 原文地址:https://www.cnblogs.com/jokerjason/p/5733131.html
Copyright © 2011-2022 走看看