Struts2-基本环境搭建和测试-day01--20200328

一、搭建环境

1.下载Struts2
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.环境搭建
新建项目
在这里插入图片描述
在这里插入图片描述
解压下载的Struts2,到目录struts2-rest-showcase.war\WEB-INF\lib里面找到需要导入的jar包。
在这里插入图片描述把jar包复制到对应工程的目录下面。
在这里插入图片描述
这不是strust2的最小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_2_5.xsd"
 id="WebApp_ID" version="2.5"> 
    <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>

加入struts2的配置文件。
在这里插入图片描述

<?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>

</struts>

配置源代码提示,window->property
在这里插入图片描述

二、实例测试

1.准备默认访问页面,启动服务器,经测试框架已经搭好
在这里插入图片描述
2.框架使用初探
struts.xml文件的配置内同如下:

<?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.action.extension" value="action,do"></constant>
  <package name="helloWorld" namespace="/" extends="struts-default">
   <action name="product-input">
      <result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
     </action>
     <action name="product-save" class="com.atguigu.struts2.helloworld.Product"
      method="save">
      <result name="details">/WEB-INF/pages/details.jsp</result> 
     </action>
  </package>
</struts>

在这里插入图片描述
index.jsp文件代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
 
 <a href="product-input.action">Product Input</a>
 
 <br><br>
 
 <a href="test.action">Test</a>
</body>
</html>

product.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="product-save.action" method="post">
ProductName: <input type="text" name="productName"/>
<br><br>
ProductDesc: <input type="text" name="productDesc"/>
<br><br>
ProductPrice: <input type="text" name="productPrice" />
<br><br>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

details.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <br><br>
ProductName:${productName }
 <br><br>
ProductDesc: ${productDesc }
 <br><br>
ProductPrice: ${productPrice }
 <br><br>
productId: ${productId }
</body>
</html>
三、Struts2基本功能说明

Struts2基本功能说明
1.Struts2使用了一个过滤器作为控制器
2.Struts2中,HTML表单将被直接映射到一个POJO.
3.Struts2中的验证逻辑编写在Action中.
4.Struts2中任何一个POJO都可以是一个Action类.
5.Struts2在页面里使用OGNL来显示各种对象模型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值