zoukankan      html  css  js  c++  java
  • struts2入门实例

    引言:  

    接触.net有3、4年的时间了,一直想学习java,中间因为种种原因耽搁下来。本人学习java的目的,一是多条出路,二是和.net平台互相印证,毕竟只用一门语言,无论是在框架还是在眼界方面都会有局限,因此在看过java基本语法后,迫不及待的想看看java的SSH框架都是做什么用的。本文是在网上copy的一个简单项目,最后经历千辛万苦,终于能运行起来了,其中的问题会在博文的最后描述下。首先说明下,本文只是介绍如何搭建struts2,对其内部运行的基本原理,等稍后更深入的理解后再做补充。

    首先列举目录结构(深受其害),以及用到的jar包:

    创建项目的各个部分:

    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.ng.filter.StrutsPrepareAndExecuteFilter
          </filter-class>
       </filter>
    
       <filter-mapping>
          <filter-name>struts2</filter-name>
          <url-pattern>/*</url-pattern>
       </filter-mapping>
    </web-app>
    View Code

    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="com" extends="struts-default">     
          <action name="hello" 
                class="com.yiibai.struts2.HelloWorldAction" 
                method="execute">
                <result name="success">/HelloWorld.jsp</result>
          </action>
       </package>
    </struts>
    View Code

    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>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>hello</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>
    View Code

    HelloWorld.jsp:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>helloworld</title>
    </head>
    <body>
        Hello World, <s:property value="name"/>
    </body>
    </html>
    View Code

    HelloWorldAction.java

    package com.yiibai.struts2;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class HelloWorldAction extends ActionSupport {
        private String name;
    
           @Override
           public String execute() throws Exception {
              return "success";
            //return SUCCESS;
           }
           
           public String getName() {
              return name;
           }
    
           public void setName(String name) {
              this.name = name;
           }
    }
    View Code

    代码已经全部贴上去了,整个struts可以说是通过过滤器action,直接转到相应的界面上。

    出现的经典问题,相信绝大多数的童鞋们都遇到过:

    404,作为过来人肯定知道是找到对应的资源,关键是应该怎么放?网上搜索了千百遍,尝试将web.xml存放的位置做修改,没有想到解决了,哈哈。困扰了2个星期。

    解决:web.xml文件本来在WebContent下面放着,最后转移到其下面的WEB-INF下面。解决了,具体是什么原因,还是以后理解了,再补充吧。

    、、

    、、

    、、

  • 相关阅读:
    Tomcat7,Tomcat8 的manager 配置
    JavaScript 获得当前日期+时间
    CentOS 安装jdk 1.8
    nginx文件结构与解析,例子
    jxl解析多个excel工作表-java代码
    一个上传图片,预览图片的小demo
    利用DES,C#加密,Java解密代码
    CentOS安装Nginx
    摄像机标定--很懒的方法
    slam-g2o安装失败的解决问题
  • 原文地址:https://www.cnblogs.com/ysyn/p/6489660.html
Copyright © 2011-2022 走看看