【问题标题】:Issue with Spring Form Tag in spring MVCSpring MVC 中的 Spring Form 标签问题
【发布时间】:2018-02-04 09:03:22
【问题描述】:

我正在尝试构建一个简单的应用程序来使用 Spring MVC 创建和更新少量记录。但由于我面临的问题是我的弹簧表单标签有一些错误,并且我无法从 2 天开始弄清楚,因此无法继续进行。错误说:

java.lang.IllegalStateException: No WebApplicationContext found: not in an DispatcherServlet request and no ContextLoaderListener register?

还有一些疑问:

  1. 为什么我在使用 dispatcherServlet 时需要 ContextLoaderListener。

  2. 每个 DispatcherServlet 都有自己的或实例化 WebApplicationContext。那为什么错误说它不可用?

  3. 我在概念上或程序上遗漏了什么?

Welcome.jsp

<html>
<body>
<h2>Welcome to the ADStore Portal</h2>
<a href="WEB-INF/views/addEmp.jsp">Add Employee</a><br>
<!-- <a href="updateEmployee.jsp">Update Employee</a> -->
</body>
</html>

addEmp.jsp

    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>Add Employee</h1>
    <form:form action = "/add" modelAttribute = "employee">
        <table>
            <tr>
                <td>Name :</td>
                <td><form:input path="empname"/>
            </tr>
            <tr>
                <td>Id :</td>
                <td><form:input path="empid"/>
            </tr>

            <tr>
                <td>Designation :</td>
                <td><form:input path="designation"/>
            </tr>

            <tr>
                <td>Department :</td>
                <td><form:input path="department"/>
            </tr>

            <tr>
                <td><input type="submit" name="Submit"/></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>AD Store</display-name>
    <servlet>
        <servlet-name>adstore</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>adstore</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

adstore-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven/>
    <context:annotation-config/>
    <context:component-scan base-package="com.adstore" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

EmployeeController.java

    package com.adstore.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.adstore.bean.Employee;
import com.adstore.dao.EmployeeDAO;

@Controller
public class EmployeeController {
    private EmployeeDAO dao; 

    @RequestMapping(value="/add")
    public ModelAndView saveEmployee(@ModelAttribute("employee") Employee emp) {
        System.out.println("In saveEmployee");
        dao.saveEmp(emp);
        return new ModelAndView("viewEmployee","command",new Employee());
    }
}

EmployeeDAO.java

    package com.adstore.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;

import com.adstore.bean.Employee;

public class EmployeeDAO {
    private JdbcTemplate jdbcTemplate;

    public boolean saveEmp(final Employee emp) {
        boolean result = false;
        String query = "INSERT INTO ADSTORE VALUES(?,?,?,?)";
        result = jdbcTemplate.execute(query, new PreparedStatementCallback<Boolean>() {

            public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                ps.setString(1, emp.getEmpName());
                ps.setInt(2, emp.getEmpId());
                ps.setString(3, emp.getDesignation());
                ps.setString(4, emp.getDepartment());

                return ps.execute();
            }
        });

        return result;
    }
}

错误图片::

【问题讨论】:

  • 包含 EmployeeController 的代码
  • 我已经更新了上面的代码并添加了 2 个类。请检查。谢谢。

标签: spring jsp spring-mvc spring-form


【解决方案1】:

没有找到应用程序上下文,因为当您直接进入 JSP 页面时,DispatcherServlet 没有处理请求。因此不需要 Spring 基础设施来处理 form:form 标签。

您应该使用@Controller 中定义的 url 访问页面,使用 @RequestMapping 或其变体。将 JSP 页面放在一个可以让客户端直接访问它们的位置并不是一个好的做法。相反,您应该将它们隐藏在 /WEB-INF/ 目录中

根据您的视图解析器,jsp 页面应位于 /WEB-INF/views 文件夹中。将 JSP 移动到此文件夹。

【讨论】:

  • 嗨,我之前尝试过,但它给了我 404 错误。
  • 我已经更新了上面的代码并添加了 2 个类。请检查。谢谢。
【解决方案2】:

您需要在 web.xml 中添加以下代码

 <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>/WEB-INF/views/*</url-pattern>
 </servlet-mapping>

 <listener>
    <listener-class>
       org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-security.xml //if you need
                /WEB-INF/adstore-servlet.xml
    </param-value>
</context-param>

关于你的疑问-

1- 如果您想将您的 Servlet 文件以自定义名称或放在您的自定义位置,而不是默认命名约定 [servletname]-servlet.xml 和 Web-INF/ 下的路径,那么您可以使用 ContextLoaderListener。

基本上,您可以使用 ContextLoaderListner 隔离根应用程序上下文和 Web 应用程序上下文。

使用上下文参数映射的配置文件将表现为根应用程序上下文配置。并且使用调度程序 servlet 映射的配置文件的行为类似于 Web 应用程序上下文。

在任何 Web 应用程序中,我们可能有多个调度程序 servlet,因此有多个 Web 应用程序上下文。

但是在任何 Web 应用程序中,我们可能只有一个根应用程序上下文,它与所有 Web 应用程序上下文共享。

我们应该在根应用程序上下文中定义我们的公共服务、实体等。控制器、拦截器等都在相关的 Web 应用程序上下文中。

还有更多信息—— 博客“Purpose of ContextLoaderListener – Spring MVC”给出了很好的解释。

【讨论】:

  • 在访问 addEmp.jsp 时仍然得到 404
  • 根据您的视图解析器,jsp 页面应位于 /WEB-INF/views 文件夹中。将 JSP 移动到此文件夹。
  • 除了welcome.jsp,这是我打的第一个jsp,其余的都在/WEB-INF/views里面
  • 我在 web.xml 中添加了一些代码,将其添加到您的 web.xml 中,另外还添加了您的文件夹结构。
【解决方案3】:

您发布的代码绝对可以正常工作。

这里发布的内容没有任何例外。但我可以指出的几点。

  1. 检查您在项目中的文件应该与下面的完全相同
  2. 您可以直接在开头调用 addEmp.jsp 来替换您的代码。

    @RequestMapping(value = "/", method = RequestMethod.GET) public String printWelcome(@ModelAttribute("employee") Employee emp) {

    return "addEmp";
    

    }

希望能解决。如果您仍然遇到问题,请将代码提交到 github 并分享链接。

【讨论】:

  • 如何直接调用 addEmp.jsp。它位于 WEB-INF 文件夹中,因此必须通过 servlet 调用它。对吧?
  • 每当像这样调用应用程序时 --- localhost:8080/<Application-Name> - 如果您将我的代码放在第二步中,这将调用您的控制器。那就是调用你的jsp。
猜你喜欢
  • 2012-10-16
  • 2011-07-05
  • 1970-01-01
  • 2013-07-13
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多