java 如何使用 Spring 表单标签包含 jsp 页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15697998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:33:04  来源:igfitidea点击:

How to include jsp pages using Spring form tag?

javaspringjspspring-annotations

提问by Java Developer

I'm using Spring, in that I have user.jsp

我正在使用 Spring,因为我有 user.jsp

user.jsphas three separate divisons: 1. Personal, 2. Educational, 3. Awards. Each section has different forms .jsp that I have created.

user.jsp有三个独立的部门:1. 个人部门,2. 教育部门,3. 奖项。每个部分都有我创建的不同形式 .jsp。

Now I'm thinking to include these three forms into user.jspand display Model using one Controller.

现在我正在考虑将这三种形式包含在user.jsp一个控制器中并显示模型。

Here is my controller class code:

这是我的控制器类代码:

@Controller
@RequestMapping(value="profile")
public class UserProfileController {

    @RequestMapping(value="user", method=RequestMethod.GET)
    public String user(Model model) throws Exception {
        model.addAttribute("profile", new PersonalForm());

        return "profile/user";
    }

And this is my Personal.jsp file (all remaining files are the same but names are different)

这是我的 Personal.jsp 文件(所有其余文件都相同但名称不同)

So how to include these three jsp into user.jsp? Actually I'm trying but Eclipse is showing an error. Below is my error code in user.jsp:

那么如何将这三个jsp包含进去user.jsp呢?实际上我正在尝试,但 Eclipse 显示错误。以下是我的错误代码user.jsp

Fragment "profile/professional.jsp" was not found at expected path /EClass/WebContent/WEB-INF/ pages/profile/profile/professional.jsp

在预期路径 /EClass/WebContent/WEB-INF/ pages/profile/profile/professional.jsp 中未找到片段“profile/professional.jsp”

So please help me how to include and how to work with single controller?

所以请帮助我如何包含以及如何使用单个控制器?

回答by

In your User.jsp write following code...

在您的 User.jsp 中编写以下代码...

        <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<fieldset id="profile_proffiesional">
    <form:form action="profile/user" modelAttribute="profile">
        <jsp:include page="personal.jsp"></jsp:include>
        <jsp:include page="educational.jsp"></jsp:include>
        <jsp:include page="awards.jsp"></jsp:include>
    </form:form>
</fieldset>