警告:org.springframework.web.servlet.PageNotFound - 在DispatcherServlet中找不到带有URI [/ board /]的HTTP请求的映射,名称为'appServlet'

时间:2015-09-30 04:24:14

标签: java spring spring-mvc servlets

我正在尝试制作一个简单的BBS,但我遇到了这个错误,

  

警告:org.springframework.web.servlet.PageNotFound - 找不到映射   对于具有名称的DispatcherServlet中带有URI [/ board /]的HTTP请求   'appServlet'

我曾多次尝试解决此错误,但我失败了。我该如何解决这个错误?

这是我的web.xml,Controllerand和servlet-context。

我的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">

	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>EUC-KR</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- The definition of the Root Spring Container shared by all Servlets 
		and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/appServlet/servlet-context.xml 
</param-value>
	</context-param>
	<!-- 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>/</url-pattern>
	</servlet-mapping>

</web-app>

我的控制器

package com.onj.board;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.board.model.BoardDTO;
import com.board.model.CommentDTO;
import com.board.service.BoardService;
import com.board.util.EncodingHandler;
import com.board.util.PageHandler;

public class BoardMultiController extends MultiActionController {

	private BoardService boardService;
	private PageHandler pageHandler;

	public void setBoardService(BoardService boardService) {
		this.boardService = boardService;
	}

	public void setPageHandler(PageHandler pageHandler) {
		this.pageHandler = pageHandler;
	}

	ModelAndView mav = null;

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		mav = new ModelAndView();

		HttpSession session = request.getSession();
		if (session.isNew() == false) {
			session.invalidate();
		}

		List<BoardDTO> list = null;

		String boardListSelect = request.getParameter("boardListSelect");
		String boardListSearchText = request
				.getParameter("boardListSearchText");

		Map<String, Object> searchMap = new HashMap<String, Object>();

		if (boardListSearchText != null) {
			searchMap.put("boardListSearchText",
					EncodingHandler.toKor(boardListSearchText));
			searchMap.put("boardListSelect", boardListSelect);

			mav.addObject("boardListSearchText",
					EncodingHandler.toKor(boardListSearchText));
			mav.addObject("boardListSelect", boardListSelect);
		}

		String pageNumber = request.getParameter("pageNumber");
		int pageNum = 1;
		if (pageNumber != null) {
			pageNum = Integer.parseInt(pageNumber);
		}

		int totalCount = pageHandler.boardAllNumber(searchMap);

		int totalPageCount = pageHandler.boardPageCount(searchMap);

		int startPage = pageHandler.boardStartPage(pageNum);
		int endPage = pageHandler.boardEndPage(pageNum, searchMap);

		List<Object> rowNumberList = new ArrayList<Object>();
		rowNumberList = pageHandler.boardSetPageNumber(pageNum);
		searchMap.put("startRow", rowNumberList.get(0));
		searchMap.put("endRow", rowNumberList.get(1));

		list = boardService.boardList(searchMap);

		mav.addObject("pageNumber", pageNum);
		mav.addObject("boardCount", totalCount);
		mav.addObject("totalPageCount", totalPageCount);
		mav.addObject("startPage", startPage);
		mav.addObject("endPage", endPage);
		mav.addObject("list", list);

		mav.setViewName("list");

		return mav;
	}

	public ModelAndView read(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String seq = request.getParameter("seq");
		BoardDTO boardDTO = boardService.readContent(seq);

		HttpSession session = request.getSession();
		session.setAttribute("boardDTO", boardDTO);

		mav.addObject("boardDto", boardDTO);
		mav.addObject("comment", boardService.ListComment(seq));

		mav.setViewName("read");

		return mav;
	}

	public ModelAndView comment(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HttpSession session = request.getSession();
		BoardDTO boardDTO = (BoardDTO) session.getAttribute("boardDTO");

		mav = new ModelAndView("redirect:/read.html?seq=" + boardDTO.getSeq());

		CommentDTO commentDTO = new CommentDTO();
		commentDTO.setComment_name(request.getParameter("comment_name"));
		commentDTO.setComment_comm(request.getParameter("comment_comm"));
		commentDTO.setSeq(boardDTO.getSeq());

		boardService.insertComment(commentDTO);

		return mav;
	}

	public ModelAndView write(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		mav = new ModelAndView("write");
		return mav;
	}

	public ModelAndView writeOk(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		mav = new ModelAndView("redirect:/list.html");

		MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest) request;

		String name = request.getParameter("name");
		String passwd = request.getParameter("passwd");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		MultipartFile file = mpRequest.getFile("file");

		String fileName = file.getOriginalFilename();

		BoardDTO boardDTO = new BoardDTO();

		String fileDir = "D:/upload/";

		byte[] fileData;
		FileOutputStream output = null;

		if (!fileName.equals("")) {
			// 파일 저장
			try {
				fileData = file.getBytes();
				output = new FileOutputStream(fileDir + fileName);
				output.write(fileData);

			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				output.close();
			}
		} else {
			fileName = " ";
		}

		boardDTO.setName(name);
		boardDTO.setPasswd(passwd);
		boardDTO.setTitle(title);
		boardDTO.setContent(content);
		boardDTO.setFilename(fileName);

		boardService.insertBoard(boardDTO);

		return mav;
	}

	public ModelAndView updatePageGo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HttpSession session = request.getSession();
		BoardDTO boardDTO = (BoardDTO) session.getAttribute("boardDTO");

		mav = new ModelAndView("update", "boardDto", boardDTO);

		return mav;
	}

	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		HttpSession session = request.getSession();

		BoardDTO boardDTO = (BoardDTO) session.getAttribute("boardDTO");

		String name = request.getParameter("name");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String seq = boardDTO.getSeq();

		boardDTO.setName(name);
		boardDTO.setTitle(title);
		boardDTO.setContent(content);
		boardDTO.setSeq(seq);

		boardService.updateBoard(boardDTO);

		mav = new ModelAndView("redirect:/read.html?seq=" + seq);

		return mav;
	}

	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		mav = new ModelAndView();

		HttpSession session = request.getSession();
		BoardDTO boardDTO = (BoardDTO) session.getAttribute("boardDTO");
		String boardPassword = boardDTO.getPasswd();
		String passwd = request.getParameter("passwd");
		String mas = "";

		if (boardPassword.equals(passwd)) {
			boardService.deleteBoard(boardDTO.getSeq());
			mav.setViewName("redirect:/list.html");
		} else {
			mas = "비밀번호가 일치하지 않습니다.";
			mav.addObject("mas", mas);
			mav.setViewName("redirect:/read.html?seq=" + boardDTO.getSeq());
		}

		return mav;
	}

}

我的servlet-context.xml

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

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

	<!-- <context:component-scan base-package="com.onj.board" /> -->

	<!-- 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/" />

	<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<beans:property name="url"
			value="jdbc:oracle:thin:@localhost:1521:ex" />
		<beans:property name="username" value="study" />
		<beans:property name="password" value="study" />
	</beans:bean>

	<!-- 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/jsp/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>



	<!-- 넘어오는 URL에 따라 컨트롤러에서 실행될 메소드 매핑 -->
	<!-- PropertiesMethodNameResolver는 prop key로 넘어오는 url에 대해 실행할 컨트롤러의 메소드 
		정의 -->
	<beans:bean id="userControllerMethodNameResolver"
		class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
		<beans:property name="mappings">
			<beans:props>
				<beans:prop key="/list.html">list</beans:prop>
				<beans:prop key="/read.html">read</beans:prop>
				<beans:prop key="/comment.html">comment</beans:prop>
				<beans:prop key="/write.html">write</beans:prop>
				<beans:prop key="/writeOk.html">writeOk</beans:prop>
				<beans:prop key="/updatePage.html">updatePageGo</beans:prop>
				<beans:prop key="/update.html">update</beans:prop>
				<beans:prop key="/delete.html">delete</beans:prop>
			</beans:props>
		</beans:property>
	</beans:bean>

	<!-- controller mapping -->
	<beans:bean
		name="/list.html /read.html /comment.html /write.html /writeOk.html /updatePage.html /update.html /delete.html"
		class="com.onj.board.BoardMultiController">
		<beans:property name="methodNameResolver" ref="userControllerMethodNameResolver" />
		<beans:property name="boardService" ref="boardService" />
		<beans:property name="pageHandler" ref="pageHandler" />
	</beans:bean>

</beans:beans>

我需要你的建议。

由于

2 个答案:

答案 0 :(得分:1)

尝试将web.xml文件中的<url-pattern>更改为:

<url-pattern>/*</url-pattern>

您是如何部署此应用程序的?作为board.war

另外,为了让您知道,这是配置Spring MVC应用程序的一种非常古老的方式。 Spring有一些guides可用来展示现代方法。

答案 1 :(得分:1)

代码中似乎缺少以下项目:

1。servlet-context.xml中,添加

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

替换     <beans:beans .....>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd
        http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

2。为其中的方法添加@Controller注释和@RequestMappings,因为servlet-context.xml中没有找到控制器配置。

@Controller
public class BoardMultiController extends MultiActionController {

请求映射到list()方法:

@RequestMapping(value="/board", method = RequestMethod.GET)
public ModelAndView list(HttpServletRequest request,
            HttpServletResponse response) throws Exception {