使用spring mvc和sitemesh时遇到问题

时间:2011-05-06 01:52:10

标签: java spring spring-mvc sitemesh

我有一个有弹簧工作的sitemesh。 这是我的sitemesh.xml

<?xml version="1.0" encoding="UTF-8"?>

<sitemesh>
    <property name="decorators-file" value="/WEB-INF/decorators.xml" />
    <excludes file="${decorators-file}" />

    <page-parsers>
        <parser content-type="text/html"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
        <parser content-type="text/html;charset=ISO-8859-1"
            class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
    </page-parsers>

    <decorator-mappers>
        <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
            <param name="config" value="${decorators-file}" />
        </mapper>
    </decorator-mappers>
</sitemesh>

这里是decorator.xml

   <?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/decorators">

    <decorator name="main" page="main.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

这是我的控制器:

package org.itax.calculator.web.controller;

import org.itax.calculator.domain.Login;
import org.itax.calculator.domain.User;
import org.itax.calculator.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {
    private static final Logger log = LoggerFactory
            .getLogger(LoginController.class);

    @Autowired
    UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public void showForm(ModelMap model) {

        log.debug("entered showLoginForm");
        model.put("command", new Login());
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public void processForm(@ModelAttribute("command") Login command,
            BindingResult result, HttpSession session) {
        validate(command, result);
        if (result.hasErrors()) {
            return;
        }
        User user = userService.getUserByUserNameAndPassword(
                command.getUserName(), command.getPassword());
        if (user != null) {
            log.debug("user login successfully, id={}, userName={}",
                    user.getId(), user.getUserName());
            session.setAttribute("USER", user);
        }
        log.debug("entered processFormSubmission, userName={}, password={}",
                command.getUserName(), command.getPassword());
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public void logout(HttpSession session, HttpServletResponse response)
            throws IOException {
        session.invalidate();
        response.sendRedirect("login");
    }

    private void validate(Login command, BindingResult result) {

        ValidationUtils.rejectIfEmptyOrWhitespace(result, "userName",
                "common.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(result, "password",
                "common.required");
    }
}

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <servlet>
        <servlet-name>visitor</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

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

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>loginFilter</filter-name>
        <filter-class>org.itax.calculator.web.filter.LoginFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>loginFilter</filter-name>
        <servlet-name>app</servlet-name>
    </filter-mapping>


    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <jsp-config>
        <taglib>
            <taglib-uri>/spring</taglib-uri>
            <taglib-location>/WEB-INF/spring-form.tld</taglib-location>
        </taglib>

        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
            <taglib-location>/WEB-INF/c.tld</taglib-location>
        </taglib>

        <taglib>
            <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
            <taglib-location>/WEB-INF/fmt.tld</taglib-location>
        </taglib>
    </jsp-config>

</web-app>

但是当我试图访问:http://localhost:8080/iTax/app/login

我收到这些错误:

    org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:491)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:236)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.itax.calculator.web.filter.LoginFilter.doFilter(LoginFilter.java:32)

root cause

java.lang.NullPointerException
    com.opensymphony.module.sitemesh.taglib.decorator.HeadTag.doEndTag(HeadTag.java:33)
    org.apache.jsp.WEB_002dINF.jsp.login_jsp._jspx_meth_decorator_005fhead_005f0(login_jsp.java:515)
    org.apache.jsp.WEB_002dINF.jsp.login_jsp._jspService(login_jsp.java:144)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:236)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.itax.calculator.web.filter.LoginFilter.doFilter(LoginFilter.java:32)

的login.jsp

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/decorators/main.jsp" %>

<html>
<head><title><fmt:message key="login.title"/></title></head>
<body>
<form:form>
    <table>
        <tr>
            <th><fmt:message key="user.userName"/>:</th>
            <td>
                <form:input path="userName"/>
                <form:errors path="userName"/>
            </td>
        </tr>
        <tr>
            <th><fmt:message key="user.password"/>:</th>
            <td>
                <form:password path="password"/>
                <form:errors path="password"/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="<fmt:message key="login.btn.label"/>"/>
            </td>
        </tr>
    </table>
</form:form>
</body>
</html>

1 个答案:

答案 0 :(得分:4)

login.jsp中的这一行看起来很可疑:

<%@ include file="/WEB-INF/decorators/main.jsp" %>

您似乎在正在装饰的页面中包含您的SiteMesh装饰器。这看起来像是对SiteMesh如何工作的误解。它接受JSP的HTML输出,并使用装饰器JSP将其重新格式化为您的装饰规则。正在装饰的页面(在这种情况下为login.jsp)应该不知道SiteMesh在那里。

尝试从login.jsp删除该行。