• 简单创建一个完整的struts2框架小程序


    要完成一个struts2框架的搭建,

    1.首先应该从官网上下载最新的jar包,网络连接:http://struts.apache.org/download.cgi#struts2514.1,选择下载Full Distribution,这里边除了jar包外还有struts2的API。

    下载完成后,在eclipse中创建一个新的web项目。将必须的8个jar包导入webcontent目录下的web-inf的lib文件夹下,8个必须的jar包的截图如下

    注:mysql-connector-java这个jar包是连接数据库使用的。根据自己使用的数据库导入。

    2.在web.xml中添加对于struts配置,代码如下:

    <?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>test4</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
       <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern> 
      </filter-mapping>
    </web-app>

    3.自己手动创建struts.xml文件,该文件的位置最好在web-inf中创建一个classes文件夹将其放在其中,在该文件里边配置action,代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
       "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    
    <constant name="struts.devMode" value="true" />
     <constant name="struts.multipart.maxSize" value="1000000" />
    
    <package name="helloworld" extends="struts-default">
    <action name="hello" class="cn.w3cschool.struts2.HelloWorldAction" method="execute">
    
    <result name="success">
    /HelloWorld.jsp
    </result>
    </package>
    </struts>

    这里边的package name应该与程序的关系不大,所以可以随意设置,但是action name的值必须与jsp中的action所对相应的方法相同,后边的class是指的.java中的类的路径

    4.创建.java 文件,代码如下:

    public class HelloWorldAction{
       private String name;
    
       public String execute() throws Exception {
          return "success";
       }
       
       public String getName() {
          return name;
       }
    
       public void setName(String name) {
          this.name = name;
       }
    }

    5.创建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>

    注意:jsp中开头必须引用struts标签库,即:<%@ taglib prefix="s" uri="/struts-tags"%>,jsp中action中的名字应该与struts2.xml文件中的action name一致。

    最后的注意事项:在创建web项目时,struts2。xml文件不会自动创建,所以要手动创建,而且里边开头的格式基本一样,可以直接复制粘贴。

    2.在导入jar包时,不能一股脑的将其全部导入,这样有可能会引起包之间的冲突,导致运行失败,只将必须的jar包导入即可

    完成上边的几项后,便可以运行tomacat,运行的结果应该如下:

    正确运行的结果应该如上。由于刚接触struts不久,如果写的有什么问题,欢迎指教!!

  • 相关阅读:
    SSM(六)JDK动态代理和Cglib动态代理
    SSM(三)Mybatis动态SQL
    MyBatis无限级分类实现的两种方法--自关联与map集合
    idea上MyBatis第一个例子
    idea 给maven项目添加依赖(二)
    idea 创建maven项目(一)
    Java学习笔记——三层架构
    MySQL实现自动使用uuid作为主键以及解决不能调用触发器的一点思路
    Java学习笔记——MySQL创建表结构
    jQuery入门——注册事件
  • 原文地址:https://www.cnblogs.com/ljysy/p/8585752.html
Copyright © 2020-2023  润新知