如何从java Spring MVC中的另一个JSP页面打开JSP页面?

时间:2015-09-18 03:17:09

标签: java jsp spring-mvc

我想创建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我有表格。然后我输入数据并按&#34;提交&#34;。然后我有这个链接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?

1 个答案:

答案 0 :(得分:0)

在您的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

完成所有操作后,您可以传递${name}上的successpage.jsp变量,以查看表单中的值。