我正在向JSP发送Map<String entityDescription, Entity entity>来填充定义为的<form:select>

<form:form id="form" modelAttribute="pojo" action="create" method="post">
    <div>
        <label for="ftpConnection">FTP Connection: </label>
        <form:select path="ftpConnection">
            <form:options  items="${ftpList}" itemLabel="description" />
        </form:select>
    </div>
</form:form>


DTO具有FTPConnection ftpConnection字段,该字段应接收在<form:select>元素的path属性上定义的ftpConnection。

POST方法大致定义为:

@RequestMapping(value = "/parameter/create", method = RequestMethod.POST)
public String createPost(@ModelAttribute("pojo") ParameterPojo pojo, ModelMap model, Errors errors) throws IOException, Exception {
    // validate entities
    // save what has to be saved
    return "where it has to go";
}


此实例的FtpConnection bean是一个非常简单的POJO,定义为:

@Entity
@Table(name = "ftp")
public class FtpConnection extends BasePojo {

    @Column(name = "description")
    private String description;

    @Column(name = "url")
    private String url;

    @Column(name = "port")
    private String port;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;
    // getters and setters
}


DTO称为ParameterPojo是另一个非常简单的POJO:

public static class ParameterPojo {

    private String id;

    private String description;

    private String agentId;

    private FtpConnection ftpConnection;
    // getters and setters
}


我得到的唯一输出是在浏览器上,如下面的图像。

java - 如何在Spring MVC中返回JSP选择框的值?-LMLPHP

没有控制台输出。

我该如何解决?

最佳答案

好吧,我为您写了一本小诗,对我有用。

DTO是一样的

我不知道您是否为FtpConnection bean开发了自定义转换器,我想是的,但是如果您未实现此原因,则是因为在提交表单时出现spring导致400错误的原因,所以您不知道该转换器是bean在某种程度上,我向开发者提交了信息,我开发了这样的转换器:

public class FtpToStringConverter implements Converter<FtpConnection, String> {
    @Override
    public String convert(FtpConnection ftpConnection) {

        ObjectMapper objectMapper = new ObjectMapper();

        try {
            return objectMapper.writeValueAsString(ftpConnection);
        } catch (IOException e) {
            return "{}";
        }
    }
}

public class StringToFtpConverter implements Converter<String, FtpConnection> {
    @Override
    public FtpConnection convert(String s) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(new StringReader(s),FtpConnection.class);
        } catch (IOException e) {
            return null;
        }

    }
}


然后我以这种方式注册自定义转换器

@Component
public class MyDefaultConversionService extends DefaultConversionService {


    @Override
    public void addConverter(Converter<?, ?> converter) {
        super.addConverter(converter);

        super.addConverter(new StringToFtpConverter());
        super.addConverter(new FtpToStringConverter());
    }
}

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.springapp.mvc"/>

    <mvc:annotation-driven conversion-service="myDefaultConversionService"/>


    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>


然后我实现了像这样的Simpel控制器

@Controller
public class HelloController {

    @RequestMapping(value = {"/index"}, method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        List<FtpConnection> ftpConnections = new ArrayList<FtpConnection>();
        FtpConnection ftpConnection = new FtpConnection();
        ftpConnection.setUrl("url1");
        ftpConnection.setPort("22");
        ftpConnection.setUsername("user1");
        ftpConnection.setPassword("pass1");
        ftpConnection.setDescription("desc1");

        ftpConnections.add(ftpConnection);

        ftpConnection = new FtpConnection();
        ftpConnection.setUrl("url1");
        ftpConnection.setPort("21");
        ftpConnection.setUsername("user2");
        ftpConnection.setPassword("pass2");
        ftpConnection.setDescription("desc2");

        ftpConnections.add(ftpConnection);
        model.addAttribute("ftpList", ftpConnections);

        model.addAttribute("pojo",new ParamiterPojo());

        return "hello";
    }

    @RequestMapping(value = "/paramiter/create" , method = RequestMethod.POST)
    public String post(@ModelAttribute("pojo") ParamiterPojo paramiterPojo, ModelMap modelMap, Errors errors){
        System.out.println(paramiterPojo);

        return "hello";
    }
}


这是我的hello.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<body>
    <h1>${message}</h1>

<form:form modelAttribute="pojo" method="post" action="paramiter/create">
    <div>
        <form:select path="ftpConnection">
            <form:options items="${ftpList}" itemLabel="description" />
        </form:select>
    </div>
    <button> Submit</button>
</form:form>
</body>
</html>


当然,这是一个非常简单的示例,您认为这是poc。

恢复我在您的代码中发现的问题是:


没有转换器
jsp中的错误操作


我希望对我的解决方案的详细描述足够细致。

07-26 09:09