Struts2 hello world搭建&Struts知识概要整理

转载请注明出处:
牵手生活--头条新闻:笔记是整理思路方式,分享是一个美德,牵手是我的生活方式
牵手生活--:笔记是整理思路方式,分享是一个美德,牵手是我的生活方式

注:如果对idea创建Manven webapp不熟悉,可参见Spring MVC -Hello World(环境搭建)


用idea创建一个maven 的web项目Struts2HelloWorld

Struts2 hello world搭建&Struts知识概要整理_第1张图片
新建一个Maven项目-webapp
Struts2 hello world搭建&Struts知识概要整理_第2张图片
image.png

注意选择好原型:org.apache.maven.archetypes:maven-archetype-webapp

Struts2 hello world搭建&Struts知识概要整理_第3张图片
image.png

next,然后直接finish

在pom.xml中添加Struts2核心框架

        
            org.apache.struts
            struts2-core
            
            2.5.5
        
Struts2 hello world搭建&Struts知识概要整理_第4张图片
添加Strust2依赖

配置web.xml

  
  
    struts2
    
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
  

  
  
    struts2
    /*
  
Struts2 hello world搭建&Struts知识概要整理_第5张图片
image.png

resources资源目录下添加struts.xml文件

Struts2 hello world搭建&Struts知识概要整理_第6张图片
创建struts2配置文件
Struts2 hello world搭建&Struts知识概要整理_第7张图片
自动创建的struts.xml内容

在project Structure中创建java源码目录与test测试目录

Struts2 hello world搭建&Struts知识概要整理_第8张图片
创建java目录与test源码目录

创建一个HelloWorldAction 继承ActionSupport,重写execute方法

Struts2中的Action及时就是一个Controller

Struts2 hello world搭建&Struts知识概要整理_第9张图片
image.png

配置struts.xml



    
    
    
    

    
        
            /result.jsp
        

        
            /add.jsp
        

        
            /add.jsp
        

    

创建一个与struts.xml配置对应的result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title



    

This is result.jsp


struts2 hello world

在idea中发布项目并在浏览器中访问

http://localhost:8080/helloworld.action

或(发现默认的情况都会响应)

http://localhost:8080/aa/bbb/helloworld.action
Struts2 hello world搭建&Struts知识概要整理_第10张图片
显示struts2的hello world
Struts2 hello world搭建&Struts知识概要整理_第11张图片
image.png

访问不存在的Action

http://localhost:8080/aa/bbb/helloworld000.action

Struts2 hello world搭建&Struts知识概要整理_第12张图片
访问不存在的Action

到这来我们的Struts hello world的项目已经搭建完成,下面开始介绍Struts2的一些知识。来个知识概要图

Struts2 hello world搭建&Struts知识概要整理_第13张图片
Struts2知识概要图
Struts2 hello world搭建&Struts知识概要整理_第14张图片
Struts2处理流程

Struts2 动态方法调用-指定method属性

修改HelloWorldAction代码

public class HelloWorldAction extends ActionSupport {
    @Override
    public String execute() throws Exception {
        System.out.println("执行Action");
        /*return super.execute();*/
        return SUCCESS;
    }
    //helloworld.action
    public String add(){
        return SUCCESS;
    }

    //helloworld.action
    public String update(){
        return SUCCESS;
    }

    public String save(){
        return "save";
    }

}

struts.xml中(指定method调用方式)

        
        
            /add.jsp
        

        
            /update.jsp
        

url

http://localhost:8080/addAction.action
Struts2 hello world搭建&Struts知识概要整理_第15张图片
image.png

!感叹号方式(不推荐使用)

访问方法:url+action+"!"+方法名+"action"

struts_helloworld.xml配置(指定感叹号方式)

        
        
            /result.jsp
            /index.jsp
        

url

http://localhost:8080/helloworld!save.action

通配符的访问方式

访问方法:url+action+"_"+方法名+"action"

struts_helloworld.xml配置(通配符的访问方式)

        

        
            /{1}.jsp
            /{1}.jsp
        

url

http://localhost:8080/helloworld_save.action

默认action -解决当找不到对应的action时,启用默认action

struts_helloworld.xml配置(默认action)

        
        
        
            /error.jsp
        

url

http://localhost:8080/aabbcc.action
Struts2 hello world搭建&Struts知识概要整理_第16张图片
image.png

修改struts的后缀,如把.action改为.do或或其他.html伪造界面;而且支持多个后缀同时使用

    
    

Struts2 hello world搭建&Struts知识概要整理_第17张图片
在web.xml中配置

struts2—constant常量的配置常用方法

Struts2 hello world搭建&Struts知识概要整理_第18张图片
image.png

Struts2通配符详解
Struts2.5动态方法调用action is not allowed
struts.xml和struts.properties详解

Struts2 hello world搭建&Struts知识概要整理_第19张图片
image.png

struts2 接收参数--使用Action属性接收

LoginAction.java

public class LoginAction extends ActionSupport {
    private String username ;
    private String password;//注意变量名要与post提交上来的明智一致,否则会包错误


    public String login() throws Exception {
        System.out.println("使用struts的action参数方式接收参数");
        System.out.println("模拟登录操作。。。 用户名:"+username+"密码:"+password);
        return SUCCESS;
    }

    public String getUsername() {

        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

login.jsp

...


    

提示:用户名输入admin,密码输入admin正确

用户名:
密码 :

提示:用户名输入admin,密码输入admin正确
...

struts_helloworld.xml配置(action属性接收方式)

        
            /success.jsp
        

url

http://localhost:8080/login.jsp
Struts2 hello world搭建&Struts知识概要整理_第20张图片
image.png

登录后情况


Struts2 hello world搭建&Struts知识概要整理_第21张图片
登录成功看日志

struts2 接收参数--使用Domain Model接收

LoginWithModelAction.java

public class LoginWithModelAction extends ActionSupport {
    private User user;

    public String login() throws Exception {
        System.out.println("使用struts的Domain Model方式接收参数");
        System.out.println("模拟登录操作。。。 用户名:"+user.getUsername()+"密码:"+user.getPassword());
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


}

loginWithModel.jsp

...


    

使用struts的Domain Model方式接收参数

用户名:
密码 :

提示:用户名输入admin,密码输入admin正确
...

struts_helloworld.xml配置(Domain Model方式接收)

        
            /success.jsp
        

url

http://localhost:8080/loginWithModel.jsp
Struts2 hello world搭建&Struts知识概要整理_第22张图片
使用Domain Model方式接收参数
Struts2 hello world搭建&Struts知识概要整理_第23张图片
使用Domain Model方式成功登录

struts2 接收参数--使用ModelDriven接收

url

http://localhost:8080/loginWithModelDriven.jsp
Struts2 hello world搭建&Struts知识概要整理_第24张图片
使用ModelDriven方式接收参数
Struts2 hello world搭建&Struts知识概要整理_第25张图片
使用ModelDriven成功登录

LoginWithModelDrivenAction.java

public class LoginWithModelDrivenAction extends ActionSupport implements ModelDriven {
    private User user = new User(); //使用ModelDriven方式接收参数必须对其实例化

    public String login() throws Exception {
        System.out.println("使用struts的ModelDriven方式接收参数");
        System.out.println("模拟登录操作。。。 用户名:"+user.getUsername()+"密码:"+user.getPassword());

        return SUCCESS;
    }

    public User getModel() {
        return user;
    }
}

loginWithModelDriven.jsp

...

    

模拟Struts2 传递的参数中带有List使用ModelDriven接收参数的例子


传递的参数中带有List

用户名:
密码 :

提示:用户名输入admin,密码输入admin正确
...

struts_helloworld.xml配置(ModelDriven接收)

        
            /success.jsp
        

struts2 接收参数2--使用ModelDriven接收(对象带List类型)

LoginWithListInModelDrivenAction.java

public class LoginWithListInModelDrivenAction extends ActionSupport implements ModelDriven {
    private UserWithList userWithList = new UserWithList(); //使用ModelDriven方式接收参数必须对其实例化

    public String login() throws Exception {
        System.out.println("使用struts的ModelDriven方式接收参数,参数中带有List");
        System.out.println("模拟登录操作。。。 用户名:"+userWithList.getUsername()+"密码:"+userWithList.getPassword());
        System.out.println("书籍有:"+userWithList.getBookList().get(0));
        return SUCCESS;
    }


    public UserWithList getModel() {
        return userWithList;
    }
}

loginWithListInModelDriven.jsp

...

    

模拟Struts2 使用ModelDriven接收参数的例子


对象中带有List变量

用户名:
密码 :
书籍 :
java核心 :
Android 解密 :

提示:用户名输入admin,密码输入admin正确
...

struts.xml配置

        
            /success.jsp
        

url

http://localhost:8080/loginWithListInModelDriven.jsp
Struts2 hello world搭建&Struts知识概要整理_第26张图片
ModelDriven中对象含义List
Struts2 hello world搭建&Struts知识概要整理_第27张图片
image.png

Struts2的处理结果只Input

url

http://localhost:8080/loginInput.jsp

效果:
如果正确,如38则会调整到正确的登录页面,
否则会留在本页面,不过url地址会发生变化

Struts2 hello world搭建&Struts知识概要整理_第28张图片
登录界面

正确的就不截图了,把错误的截图处理


Struts2 hello world搭建&Struts知识概要整理_第29张图片
image.png
Struts2 hello world搭建&Struts知识概要整理_第30张图片
image.png

Struts2 hello world搭建&Struts知识概要整理_第31张图片
image.png

LoginInputAction.java

public class LoginInputAction extends ActionSupport implements ModelDriven{
    private User user = new User();

    public String login() throws Exception {
        if(user.getUsername() ==null
                ||"".equals(user.getUsername().trim())){
            this.addFieldError("username","用户名为空");
            return INPUT;
        }

        System.out.println("使用struts的ModelDriven方式接收参数");
        System.out.println("模拟登录操作。。。 用户名:"+user.getUsername()+"密码:"+user.getPassword());
        return SUCCESS;
    }


    public User getModel() {
        return user;
    }
}

loginInput.jsp

<%--struts扩展标签--%>
<%@ taglib prefix="s" uri="/struts-tags" %>

...

    模拟Struts2 传递的参数中带使用ModelDriven接收参数的例子
本页主要用于处理Struts2返回值之----input

演示效果是如果填入年龄(int型),

如果正确,如38则会调整到正确的登录页面()

否则会留在本页面,不过url地址会发生变化

<%--注意s标签--%> 用户名:
密码 :
年龄 :

提示:
1:年龄输入非法字符用自动返回到本页,用户名不能为空
...

struts_helloworld.xml配置(Struts2 返回值input返回值)

        
        
            /success.jsp
            /loginInput.jsp
        

遗留问题其他看一个错误
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

structs2和lo4j2的问题
java项目中Classpath路径到底指的是哪里
Intellij IDEA 配置最简单的maven-struts2环境的web项目

预留扩展知识:

  • 前端如jsp如何使用struts扩展s标签:<%@ taglib prefix="s" uri="/struts-tags" %>
    Struts2在Action中获得Response对象的四种方法
    Struts2获取Session的三种方式
    struts2在配置文件与JSP中用OGNL获取Action属性
    struts2 - View页面中获取Action的成员变量
    struts2获取request对象的四种方式

源码下载

https://yunpan.360.cn/surl_yQbyGe6wTP3 (提取码:b082)

你可能感兴趣的:(Struts2 hello world搭建&Struts知识概要整理)