欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

初学springmvc时遇到的一个问题

程序员文章站 2022-03-30 19:52:45
...
web.xml
<?xml version="1.0" encoding="UTF-8"?>
 <!-- Copyright : adobocode.com , 2010 -->
<web-app id="WebApp_ID" 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"> 
 
   <display-name>SampleSpringMVC</display-name> 
 
 <!--  载入Spring配置文件 -->
   <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>SampleSpringMVC</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
  
    <servlet-mapping>
        <servlet-name>SampleSpringMVC</servlet-name> 
        <url-pattern>*.do</url-pattern> 
    </servlet-mapping>     
  
  <filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
  
  
    <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
  
</web-app>

Controller
package com.boda.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boda.model.Department;
import com.boda.service.DepartmentService;

@Controller
public class DepartmentController {
		
	@Autowired
	private DepartmentService departmentService;
	
	@RequestMapping(value = "department.do")
	public String create() throws Exception{
		return "welcome";
	}
}

报错信息
javax.servlet.ServletException: Could not resolve view with name 'welcome' in servlet with name 'SampleSpringMVC'
	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


解决办法过滤所有请求<url-pattern>/</url-pattern>
Controller改为如下
package com.boda.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boda.model.Department;
import com.boda.service.DepartmentService;

@Controller
@RequestMapping(value = "hr/department/")
public class DepartmentController {
		
	@Autowired
	private DepartmentService departmentService;
	
	@RequestMapping(value = "add")
	public String create() throws Exception{
		return "hr/welcome";
	}
}

在WEB/INF下建立views/hr/welcome.jsp
一开始welcome.jsp在views下面,就出错,不知道何故,记录下来