如何搭建Struts2环境

第一步:加入jar包:把jar包加入到web应用的lib下面。

第二步:配置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" 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">
    <!-- 配置 Struts2 的 Filter -->
<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>

第三步:在src目录下配置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>
<!--
packeage:包,struts2使用package来组织模块
name:属性,随便什么名字都行,用于其他包引用当前包
extends:当前包继承的那个包,继承的可以继承其中的所有配置,通常情况下继承struts-default
struts-default包在struts-default.xml中定义
namespace:可选,如果他没有给出,则以/为默认值
若有namespace有一个默认值,则要想调用这个包里的Action,就必须把这个属性所定义的命名空间添加到关联
的url的字符串里,
http://localhost:8080/contextPath/namespace/actionName.action
  -->
<package name="helloword" extends="struts-default" namespace="/">
<!-- 
class:comopensymphony.xwork2.ActionSupport
method:默认值为execute
result:返回结果,表示action方法执行后可能返回一个结果,所以一个action节点可能会有多个result子节点
多个result子节点就要name来区分
name:标识一个result,和action方法的返回值对应,默认值为success
type:标书结果的类型,默认值为dispatcher
<action name="product-input"  class="comopensymphony.xwork2.ActionSupport" 
method="execute">
<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
 -->
<action name="product-input">
<result>/WEB-INF/pages/input.jsp</result>


<!-- 配置action:一个struts2的请求就是一个action
name:对应一个struts2请求的名字(或对应一个ServletPath但去除/和扩展名)不包含扩展名 -->
</action>
<action name="product-save" class="com.enity.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>

第四步:就是写Javabean和jsp了




 

猜你喜欢

转载自blog.csdn.net/qq_39093474/article/details/80263217