0

我想开始使用 Spring MVC,但无法调整一个简单的示例。我只想将一个参数从一个 jsp 传递给另一个,但是有错误:

HTTP 状态 404 - /controller/results.jsp 类型状态报告消息 /controller/results.jsp 描述 请求的资源 (/controller/results.jsp) 不可用。Apache Tomcat/7.0.12

我的 web.xml 代码:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

        <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/home</url-pattern>
        </servlet-mapping>

Servlet-context.xml

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="edu.demidov.controller" />

</beans:beans>

控制器.java

@Controller
public class HomeController extends HttpServlet{

    private static final long serialVersionUID = 4825408935018763217L;  

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);      

    EducationDaoInterface educationDaoIntfc;

    @RequestMapping(value="/home", method=RequestMethod.GET)
    public ModelAndView  firstActionPage() {    

        return new ModelAndView("home");

    }


    @RequestMapping(value = "/result.jsp", method=RequestMethod.GET)
    public String SecondActionPage(@RequestParam String firstname, Model model) throws IOException {

        model.addAttribute("myname", firstname);

        return "result";

    }

结果.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

Hello form result.jsp

First name: ${myname}

</body>
</html>

主页.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form name="myform" action="<c:url value="/results.jsp"/>" method="GET">

<input type="text"  name="firstname"/>

<input type="submit"/>

</form>

</body>
</html>

谢谢。

4

4 回答 4

1

您有错误的 URL 映射。

有两种解决方案。

1.

您可以将控制器中 SecondActionPage 函数的 url 映射更改为以下。

@RequestMapping(value = "/result", method=RequestMethod.GET)

并将 Home.jsp 中的表单操作更改为<c:url value="/results"/>

2.

您可以将 appServlet 的 url-pattern 更改为/*. 这是 Spring Web 应用程序中 url-pattern 的标准方式。然后您需要按照我在解决方案 1 中的建议更改 home.jsp 中的请求映射和操作。

我向您推荐解决方案 2。这是最佳实践。

希望这对您有所帮助。:)

于 2013-06-05T03:45:49.510 回答
0

尝试这个

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

    <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>




@RequestMapping(value="/home", method=RequestMethod.GET)
    public ModelAndView  firstActionPage() {    
    return new ModelAndView("home");
}


@RequestMapping(value = "/result", method=RequestMethod.GET)
public String SecondActionPage(@RequestParam String firstname, Model model) throws IOException {
    model.addAttribute("myname", firstname);
    return "result";
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
<body>

    <form name="myform" action="/result" method="GET">
        <input type="text"  name="firstname"/>
        <input type="submit"/>
    </form>

</body>
</html>
于 2013-06-05T10:51:10.703 回答
0

当我单击从我的家庭 jsp 到另一个控制器 url 的提交按钮时,我得到了这个:

 `http://localhost:8080/controller/result?firstname=Vadim

似乎我将所有内容都正确地传递给了我的控制器,但为什么我的 result.jsp 页面没有出现

HTTP 状态 404 - /controller/result

类型状态报告

消息/控制器/结果

描述 请求的资源 (/controller/result) 不可用。

Apache Tomcat/7.0.12

于 2013-06-05T16:00:17.473 回答
0

您不需要提供带有扩展名的 jsp 文件的完整名称,因为它将由org.springframework.web.servlet.view.InternalResourceViewResolver.

将您的表单操作更改为:

<form name="myform" action="result" method="GET"> //check spelling and remove '/' from url

控制器映射到:

@RequestMapping(value = "/result", method=RequestMethod.GET)

希望这可以帮助你。

于 2013-06-05T11:01:02.963 回答