【问题标题】:How can i open JSP page from another JSP page in java Spring MVC?java - 如何从java Spring MVC中的另一个JSP页面打开JSP页面?
【发布时间】:2015-09-18 03:17:08
【问题描述】:

我想创建 2 个 JSP 页面。我有控制器:

@Controller
@RequestMapping(value = "/api/web/users")
public class ServletWebController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "message");
        return "hello";
    }
}

它打开第一个带有表单的 JSP 页面:

<html>
<body>
    <form action="main.jsp" method="post">
        First Name: <input type="text" name="first_name">
        <br />
        Last Name: <input type="text" name="last_name" />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

当我在表单中输入数据并按下提交按钮时,我想打开第二个 JSP 页面并从表单中打印数据:

<html>
<head>
    <title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
<center>
    <h1>Using GET Method to Read Form Data</h1>
    <ul>
        <li><p><b>First Name:</b>
            <%= request.getParameter("first_name")%>
        </p></li>
        <li><p><b>Last  Name:</b>
            <%= request.getParameter("last_name")%>
        </p></li>
    </ul>
</body>
</html>

但是第二个 JSP 页面没有打开。所以我输入http://localhost:4000/api/web/users 我有表格。然后我输入数据并按“提交”。然后我有这个链接http://localhost:4000/api/web/main.jsp 和错误:

HTTP Status 404 - /api/web/main.jsp

type Status report

message /api/web/main.jsp

description The requested resource is not available.

Apache Tomcat/8.0.26

我是 Spring 和 JSP 的新手,如何从另一个 JSP 打开 JSP?

【问题讨论】:

  • 较新的jsp路径是什么?是 hello.jsp 吗?

标签: java jsp spring-mvc


【解决方案1】:

在您的Controller 中,您可以执行以下操作:

@RequestMapping(value="/successpage", method =RequestMethod.POST)
public void successpage(ModelMap model, Users user)
{
    model.addAttribute("name", user.getFirstName());
}

@RequestMapping(value="/signupform", method=RequestMethod.GET)
public ModelAndView signupForm()
{
    return new ModelAndView("user", "command", new Users()); //Object that contains your getter and setters
}

在您的表单上,您可以修改打开的Form 标签,如下所示:

<form action="/successpage" method="POST">

在您的 WEB-INF/jsp 文件夹中,您将需要 2 个页面:signupform.jspsuccesspage.jsp

说到底,您可以通过successpage.jsp 上的${name} 变量来查看表单中的值。

【讨论】:

    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 2023-04-11
    • 2018-12-20
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多