eclipse学习(第二章:初识ssh)——14.Struts2类型转换
前言
本文是通过看这个网站后自己并实践后做的笔记
https://www.w3cschool.cn/struts_2/struts_type_conversion.html
为什么需要类型转换?
因为有时候,你需要获取的值并是自己定义的数据类型,类似这样
private Environment environment = new Environment("Test");
像这些自己定义的数据类型,如果你直接获取对应的值,你得到的只能是它的一个地址,所以这个时候就需要用到类型转换。
创建项目
1、初始化项目以及jar包拉取
2、创建一个Environment 类
等下会创建一个处理业务SystemContent的类来new一个它,从而实现自定义数据类型的样子
package com.czx.type;
public class Environment {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
public Environment(String name) {
super();
this.name = name;
}
}
3、创建SystemContent 类
该类自定义两个变量,一个是String 类型的,一个是Environment 类型的。
package com.czx.type;
import com.opensymphony.xwork2.ActionSupport;
public class SystemContent extends ActionSupport {
public Environment getEnvironment() {
return environment;
}
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public String getSystemName() {
return systemName;
}
public void setSystemName(String systemName) {
this.systemName = systemName;
}
//设置环境为开发环境
private Environment environment = new Environment("Test");
private String systemName = "windows 7";
@Override
public String execute() throws Exception {
return "success";
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
4、修改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">
<display-name>ssh_learn_type_change</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.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>
5、创建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>
<constant name="struts.devMode" value="true"></constant>
<package name="environment" extends="struts-default">
<action name="typeChange" class="com.czx.type.SystemContent" method="execute">
<result name="success">/system.jsp</result>
</action>
</package>
</struts>
6、创建system.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
获取环境情况:<s:property value="environment"/><br/>
获取操作系统名称:<s:property value="systemName"/>
</body>
</html>
7、测试
访问:
http://localhost:8080/ssh_learn_type_change/typeChange
测试结果如下,显而易见这里的environment我们并不能直接得到我们想要的值,那么就要进行转换。其实默认会去调用Environment的toString()方法
项目进行转换方法
1、直接加入toString()方法
修改Environment
package com.czx.type;
public class Environment {
@Override
public String toString() {
// TODO Auto-generated method stub
return "环境里面的内容为:"+name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
public Environment(String name) {
super();
this.name = name;
}
}
再次访问http://localhost:8080/ssh_learn_type_change/typeChange
2、转换注册方式A(局部注册,只针对单个类的Environment )
修改之前记得把之前在Environment 加入的tostring方法删掉哟,其实代码生成器一键生成toString就没有问题了,学了第一个方法也够了,下面的是拓展。
创建EnvironmentConverter 类,覆盖convertFromString()和convertToString()两个方法将Environment转换为String。这个类名随便取,但是最后还是有点格式会好些,因为后面有位置指定。
package com.czx.type;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
//默认继承struts框架的类型转换类,然后进行处理
public class EnvironmentConverter extends StrutsTypeConverter{
@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
//这里先返回环境
Environment env = new Environment(arg1[0]);
return env;
}
@Override
public String convertToString(Map arg0, Object arg1) {
//返回转换后的数据
Environment env = (Environment) arg1;
return env == null ? null : env.getName();
}
}
创建SystemContent-conversion.properties
这里有格式要求,’[action-class]’-conversion.properties的属性文件。文件内容的话,“environment”是SystemContent .java类中属性的名称,用以告诉Struts使用EnvironmentConverter来转换这个属性。位置可以跟类同级。
environment=com.czx.type.EnvironmentConverter
测试结果:
3、转换注册方式B(全局注册)
创建EnvironmentConverter 类,覆盖convertFromString()和convertToString()两个方法将Environment转换为String。这个类名随便取,但是最后还是有点格式会好些,因为后面有位置指定。
package com.czx.type;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
//默认继承struts框架的类型转换类,然后进行处理
public class EnvironmentConverter extends StrutsTypeConverter{
@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
//这里先返回环境
Environment env = new Environment(arg1[0]);
return env;
}
@Override
public String convertToString(Map arg0, Object arg1) {
//返回转换后的数据
Environment env = (Environment) arg1;
return env == null ? null : env.getName();
}
}
xwork-conversion.properties
这里指定了对应的类所对应的类转换器,无论是哪个类使用Environment都可以转换了。位置应该在WEB-INF/classes中创建。
com.czx.type.Environment=com.czx.type.EnvironmentConverter
测试访问http://localhost:8080/ssh_learn_type_change/typeChange
项目地址
这个仓库里面的ssh_learn_type_change
https://gitee.com/mrchen13427566118/ssh_learn.git
如果不会怎么弄到eclipse的话,就看这里
https://blog.csdn.net/weixin_43987277/article/details/116936221