【问题标题】:Is it possible to output a drop-down list with values from DB in an easier way?是否可以以更简单的方式输出包含 DB 值的下拉列表?
【发布时间】:2018-03-05 22:44:42
【问题描述】:

这种方式不是最好的。我正在寻找更好的方法。没有Lists可以吗?

我的页面控制器:

public class CreateUserPageController {

@Autowired
private UserServiceImpl userServiceImpl;


@RequestMapping(value = "/create-user", method = RequestMethod.GET)
public ModelAndView showForm() {
    ModelAndView model = new ModelAndView("admin/create-user");
    model.addObject("user", new User());
    UserRoleDTO[] roles = UserRoleDTO.values();
    List<String> roleNames = new ArrayList<>();
    for (UserRoleDTO role : roles) {
        roleNames.add(role.getName());
    }
    model.addObject("roleNames", roleNames);
    return model;
}

@RequestMapping(value = "/create-user", method = RequestMethod.POST)
public ModelAndView submitForm(@ModelAttribute("user") User user) {
    userServiceImpl.create(user);
    return showForm();
    }
}

我的 JSP:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
<head>
    <title>Create test</title>
</head>
<body>

<h1>Create Test</h1>
<form:form method="POST" action="/create-test" modelAttribute="topicTestDTO">
    <form:input path="topic.name" type="text" list="topic_list" placeholder="choose or create topic"/>
    <datalist id="topic_list">
        <c:forEach items="${topicList}" var="topic">
            <option>${topic}</option>
        </c:forEach>
    </datalist>
    <br>
    <form:input path="test.name" type="text" placeholder="create test name"/>
    <input type="submit">
</form:form>
</body>
</html>

感谢您的帮助。 如果您需要更多信息,请告诉我哪一个,我将完成代码。

【问题讨论】:

  • 请问您想要什么样的改进,您这样做的目的是什么?

标签: java spring hibernate jsp


【解决方案1】:

这里的问题是它去数据库检索数据的每个请求,即使数据没有改变也会降低应用程序的性能。

通过使用本地缓存我们可以提高性能,因此在应用程序加载它时,会从数据库中查询自己的数据并将其存储在缓存中,然后我们可以重用缓存中的数据。

【讨论】:

    猜你喜欢
    • 2011-08-26
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2017-04-28
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多