使用Spring MVC批注配置和JSP

时间:2017-06-26 21:02:03

标签: java jsp spring-mvc

我正在通过书籍学习Spring MVC。所以当我输入此网址http://localhost:8080/webstore/market/products/add时。在浏览器上我收到一个错误。此外,我在此目录下有一个名为messages.properties的外部文件:/webstore/src/main/resources/messages.properties,其中包含以下内容:addProduct.form.productId.label =新产品ID。基本上,我正在尝试外化我的文件addProduct.jsp的标签。这是我的文件

@Configuration
@EnableWebMvc
@ComponentScan("com.packt.webstore")
public class WebApplicationContextConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");

        return resolver;
    }

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);

        configurer.setUrlPathHelper(urlPathHelper);
    }

    // Added for -> Externalizing text messages
    @Bean
    public MessageSource messageResource() {
        ResourceBundleMessageSource resource = new ResourceBundleMessageSource();
        resource.setBasename("messages");
        resource.setDefaultEncoding("UTF-8");
        return resource;
    }
}

addProduct.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet"
    href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<title>Products</title>
</head>
<body>
    <section>
    <div class="jumbotron">
        <div class="container">
            <h1>Products</h1>
            <p>Add products</p>
        </div>
    </div>
    </section>

    <section class="container">
     <form:form method="POST" modelAttribute="newProduct" class="form-horizontal">
        <fieldset>
            <legend>Add new product</legend>

            <div class="form-group">
                <label for="productId" class="control-label col-lg-2 col-lg-2">
                <spring:message code="addProduct.form.productId.label"/></label>
                <div class="col-lg-10">
                    <form:input id="productId" path="productId" type="text"
                        class="form:input-large" />
                </div>
            </div>

            <div class="form-group">
                <label for="name" class="control-label col-lg-2 col-lg-2">Product
                    Name</label>
                <div class="col-lg-10">
                    <form:input id="name" path="name" type="text"
                        class="form:input-large" />
                </div>
            </div>

            <div class="form-group">
                <label for="unitPrice" class="control-label col-lg-2 col-lg-2">Unit Price</label>
                <div class="col-lg-10">
                    <form:input id="unitPrice" path="unitPrice" type="text"
                        class="form:input-large" />
                </div>
            </div>
            <div class="form-group">
                <label for="manufacturer" class="control-label col-lg-2 col-lg-2">Manufacturer</label>
                <div class="col-lg-10">
                    <form:input id="manufacturer" path="manufacturer" type="text"
                        class="form:input-large" />
                </div>
            </div>

            <div class="form-group">
                <label for="category" class="control-label col-lg-2 col-lg-2">Category</label>
                <div class="col-lg-10">
                    <form:input id="category" path="category" type="text"
                        class="form:input-large" />
                </div>
            </div>

            <div class="form-group">
                <label for="unitsInStock" class="control-label col-lg-2 col-lg-2">Unit in Stock</label>
                <div class="col-lg-10">
                    <form:input id="unitsInStock" path="unitsInStock" type="text"
                        class="form:input-large" />
                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="description">Description</label>
                <div class="col-lg-10">
                    <form:textarea id="description" path="description" rows="2" />
                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-lg-2" for="condition">Condition</label>
                <div class="col-lg-10">
                    <form:radiobutton path="condition" value="New" />New
                    <form:radiobutton path="condition" value="Old" />Old
                    <form:radiobutton path="condition" value="Refurbished" />Refurbished

                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-10">
                    <input type="submit" id="btnAdd" class="btn btn-primary"
                        value="Add">
                </div>
            </div>

        </fieldset>

    </form:form>
     </section>
</body>
</html>

浏览器出错:

message javax.servlet.ServletException:javax.servlet.jsp.JspTagException:在code&#39; addProduct.form.productId.label&#39;下找不到任何消息。对于语言环境&#39; ru_RU&#39;。

说明服务器遇到内部错误,导致无法完成此请求。

例外

org.apache.jasper.JasperException:javax.servlet.ServletException:javax.servlet.jsp.JspTagException:在代码&#39; addProduct.form.productId.label&#39;下找不到任何消息。对于语言环境&#39; ru_RU&#39;。     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:565)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)     javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)     org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)     org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1282)     org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)     javax.servlet.http.HttpServlet.service(HttpServlet.java:622)     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

0 个答案:

没有答案