Struts2搭建

1.IDEA配置maven、tomcat( https://blog.csdn.net/weixin_41715878/article/details/83211564)
2.导包

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.3.4</version>
</dependency>

3.在web.xml配置过滤器

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>Archetype Created Web Application</display-name>

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

4.在resources下新建、配置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>
    <!--在struts配置太多的package查看不方便,通过再写一个test.xml(任意名字),引入到struts.xml中-->
    <!--test.xml配置同样需要约束-->
    <!--<include file="test.xml"/>-->

    <!-- namespace="/" 表示请求中action="/hello.action"中的/  -->
    <package name="default" extends="struts-default" namespace="/">
        <!--如果没有找到指定的action,会跳转到错误页面,default-action-ref需要配置在package里面第一行-->
        <!--name指定一个action-->
        <default-action-ref name="myError"/>

        <!--全局结果集,必须在action节点上面配置-->
        <global-results>
            <!-- 当使用action时,内部没有result时,就会调用全局的result-->
            <result name="index">/index.jsp</result>
        </global-results>

        <!-- name="hello"  对应请求中actino="/hello -->
        <action name="show" class="com.ssh.action.UserAction" method="show">
            <!-- name="success"对应show方法中的返回值 -->
            <!-- type='dispatcher' 默认结果类型,forword请求跳转到另一个jsp-->
            <!-- type='chain'	    请求跳转到另一个action-->
            <!-- type='redirect'   重定向一个jsp页面-->
            <!-- type='redirectAction' 重定向一个action-->
            <!-- type='stream'		将元素数据作为流传递回浏览器,该结果类型对下载的内容和图片非常有用-->
            <result name="success" type="dispatcher">/WEB-INF/main.jsp</result>
        </action>

        <!--使用通配符方式配置执行方法-->
        <!--{1}表示第一个通配符,可以在result中共用-->
        <!--<action name="show_*_*_*" class="com.ssh.action.{1}" method="{2}">
            <result>/{3}.jsp</result>
        </action>-->

        <action name="myError">
            <result>/error.jsp</result>
        </action>

    </package>
</struts>

3.编写Action类

public class UserAction implements ModelDriven<User> {

    /*使用模型驱动方式,可以直接把表单数据封装到实体类的对象里面*/
    /*需要实现类ModelDriven<User>,和方法getModel*/
    /*form表单中input的name值需要和user成员变量名称一致*/
    private User user = new User();
    @Override
    public User getModel() {
        return user;
    }

    public String show(){
        /*action获取表单的三种方式*/

        /*ActionContext中保存了Action执行所需要的所有对象,包括parameters、request、session、application等*/
        /*ActionContext context = ActionContext.getContext();
        HttpSession session = context.getSession();*/

        /*ServletActionContext类,可以获得域对象*/
       /* HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        ServletContext context = ServletActionContext.getServletContext();
        PageContext pageContext = ServletActionContext.getPageContext();*/

       /*接口注入ServletRequestAware、ServletResponseAware、SessionAware、ServletContextAware*/
       /*略*/



        return "success";
    }

    public String test(){
        System.out.println(user);
        return "success";
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建Struts2项目需要按照以下步骤进行: 1. 创建Maven项目 在命令行中执行以下命令,创建一个Maven项目: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=my-struts2-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false ``` 其中,`-DgroupId`表示项目的groupId,`-DartifactId`表示项目的artifactId,`-DarchetypeArtifactId`表示使用的模板,这里使用的是maven-archetype-webapp。 2. 添加Struts2依赖 在`pom.xml`文件中添加Struts2依赖: ```xml <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.26</version> </dependency> ``` 3. 创建Struts2配置文件 在`src/main/resources`目录下创建`struts.xml`文件,配置Struts2的基本信息,例如: ```xml <!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" /> <package name="default" extends="struts-default"> <action name="hello" class="com.example.MyAction"> <result>/index.jsp</result> </action> </package> </struts> ``` 4. 创建Struts2 Action 在`src/main/java`目录下创建一个Java类,例如`MyAction.java`,作为Struts2的Action,例如: ```java package com.example; import com.opensymphony.xwork2.ActionSupport; public class MyAction extends ActionSupport { private String message; public String execute() { message = "Hello, Struts2!"; return SUCCESS; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } ``` 5. 创建JSP页面 在`src/main/webapp`目录下创建一个JSP页面,例如`index.jsp`,用于显示Action的返回结果: ```html <html> <head> <title>Struts2 Example</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 6. 运行项目 使用以下命令在Tomcat中运行项目: ``` mvn tomcat7:run ``` 然后在浏览器中访问`http://localhost:8080/my-struts2-app/hello`即可看到页面上显示的内容"Hello, Struts2!"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值