有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不会切换到另一个。单击submit时使用jsp。春季mvc

我的控制器。当我打开浏览器时,起始页开始。jsp和createmodel用户,其中有一个字段(字符串名)和set,get方法。一开始。jsp拥有文本字段并提交“登录”

@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView start() {
        return new ModelAndView("start", "user", new User());
    }

    @RequestMapping(value = "/input", method = RequestMethod.POST)
    public ModelAndView input(@ModelAttribute("user") User user) {
        return new ModelAndView("input", "userName", user);
    }
}

我的起始页。有文本字段并提交

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
    <title>start</title>
</head>
<body>
<h1>TEST</h1>

<form:form method="post" commandName="user" action="input">
    <input type="text" name="name"><br>
    <input type="submit" value="LOGIN">
</form:form>
</body>
</html>

我的问题:我打开localhost:8080/webapp并输入名称。目前正在创建模型用户示例。我按下“LOGIN”键,打开localhost:8080/input,其中“input.jsp”是两个页面,输出“helloworld,{name}”。但如果我在start中替换action=“input”。jsp并使用action=“webapp\input”则一切正常。但我认为这是个坏主意


共 (1) 个答案

  1. # 1 楼答案

    我看你的代码没有任何问题。我试着设置这个例子,在下面的配置中效果很好

    webappservlet。xml:

    <context:component-scan base-package = "com.test" />
    
       <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name = "prefix" value = "/WEB-INF/jsp/" />
          <property name = "suffix" value = ".jsp" />
       </bean>
    

    JSP位置:

    \WEB-INF\jsp\input.jsp
    \WEB-INF\jsp\start.jsp
    

    样本输入。jsp:

    <%@ page import="com.test.*" %>
    <html>
    <head>
        <title>start</title>
    </head>
    <body>
    <h1>TEST</h1>
    <%
        User user   = (User)request.getAttribute("userName");
    %>
     Hello - <%=user.getName() %>
    </body>
    </html>
    

    start.jspHomeController中无需更改