Struts2 简单页面实现 Converter转换器使用

在这里插入图片描述
在这里插入图片描述

问题记录

  • JAVA web项目中的no result defined for action xxx Action and result input 错误
  • java.lang.NoSuchMethodException: com.example.ssh3.LoginAction.setUser([Ljava.lang.String;)
  • ognl.MethodFailedException: Method “setUser” failed for object com.example.ssh3.LoginAction@770c65ba [java.lang.NoSuchMethodException: com.example.ssh3.LoginAction.setUser([Ljava.lang.String;)]

这几个博客给了我启发,但我不是按照博客上面解决的

博客1
博客2
我的问题是,缺少了一个文件xwork-conversion.porperties
在里面添加

com.example.domain.User = com.example.Converter.UserConverter

然后三个报错一起解决
还有问题或许可以在这上面解决
https://struts.apache.org/core-developers/index.html

代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>ssh3</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>ssh3</name>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.7.0</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.1.8.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core -->
        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ognl/ognl -->
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.0.21</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jboss/javassist -->
        <dependency>
            <groupId>jboss</groupId>
            <artifactId>javassist</artifactId>
            <version>3.7.ga</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.0</version>
            </plugin>
        </plugins>
    </build>
</project>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <package name="default" extends="struts-default">
        <action name="login" method="execute" class="com.example.ssh3.LoginAction">
            <result name="success" type="dispatcher">hello.jsp</result>
            <result name="error" type="dispatcher">login.jsp</result>
<!--            <result name="input">hello.jsp</result>-->
        </action>
    </package>

    <!-- Add packages here -->

</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

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

xwork-conversion.porperties

com.example.domain.User = com.example.Converter.UserConverter

hello.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: 13224
  Date: 2021/2/25
  Time: 19:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
<%--<h1>你好, ${username}</h1>--%>
<h1>你好, <s:property value="user.username"/></h1>
</body>
</html>

login.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: 13224
  Date: 2021/2/25
  Time: 19:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<s:fielderror/>
<html>
<head>
    <title>登陆</title>
</head>
<body>

<%--下面的login.action中的login,表示struts.xml里的action也叫login--%>
<s:form action="login" enctype = "multipart/form-data" method="POST">
    <%--    下面的name和LoginAction中的字段保持一致--%>
    <s:textfield name="user" label="用户名"/>
    <s:password name="user" label="密码"/>
    <s:submit value="登陆" />
</s:form>

</body>
</html>

LoginAction.java

package com.example.ssh3;

import com.example.domain.User;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Objects;

/**
 * @Description
 * @Author Cai
 * @Date 2021-02-25 19:36
 */
public class LoginAction extends ActionSupport {
    private User user;

    public User getUser() {
        return user;
    }

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

    @Override
    public String execute() throws Exception {
        System.out.println("111");
        return login();
    }

    public String login() {
        if (Objects.equals(user.getUsername(), "Cai") && Objects.equals(user.getPassword(), "123456")) {
            return "success";
        } else {
            return "error";
        }
    }
}


User.java

package com.example.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description
 * @Author Cai
 * @Date 2021-02-26 17:04
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String username;
    private String password;
}

UserConverTer.java

package com.example.Converter;

import com.example.domain.User;
import ognl.DefaultTypeConverter;

import java.util.Map;

/**
 * @Description
 * @Author Cai
 * @Date 2021-02-26 17:07
 */
public class UserConverter extends DefaultTypeConverter {
    @Override
    public Object convertValue(Map context, Object value, Class toType) {
        //需要将字符串向User类型转换时
        if (toType == User.class) {
            //系统的请求参数是一个字符串数组
            System.out.println("TestType");
            String[] params = (String[]) value;
            return new User(params[0],params[1]);
        } else if (toType == String.class) {
            User user = (User) value;
            System.out.println("--------------------");
            return user.getUsername() + " " + user.getPassword();
        }
        System.out.println("--------------------");
        return null;
//        return super.convertValue(context, value, toType);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值