Java InternalResourceViewResolver未解析
正如我对Spring所做的那样,我从来没有对SpringMVC做过任何事情,我想尝试一下,看看它与Grails和Rails等我更了解的东西相比如何。我有一个简单的应用程序,只有一个JSP和一个控制器端点,但我无法让SpringMVC解析到JSP的路径 以下是web-INF中的web.xml:Java InternalResourceViewResolver未解析,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,正如我对Spring所做的那样,我从来没有对SpringMVC做过任何事情,我想尝试一下,看看它与Grails和Rails等我更了解的东西相比如何。我有一个简单的应用程序,只有一个JSP和一个控制器端点,但我无法让SpringMVC解析到JSP的路径 以下是web-INF中的web.xml: <beans...> <context:component-scan base-package="com.springapp"/> <context:p
<beans...>
<context:component-scan base-package="com.springapp"/>
<context:property-placeholder location="classpath*:my.properties"/>
<mvc:resources mapping="/webjars/**" location="/webjars/"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
</beans>
同时,WEB-INF/jsp中有一个名为hello.jsp的文件
当我在应用程序中导航到“/”时,我得到一个404,当时我想我会得到hello.jsp
我还有一个集成测试,通过404失败确认了同样的问题:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/dispatcher-servlet.xml")
public class AppIT {
private MockMvc mockMvc;
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
protected WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@Test
public void testMVC() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("hello"));
}
}
我确信我遗漏了一些明显的东西,因此我希望Spring MVC专家告诉我这是什么。您必须将
添加到Spring配置中,这会告诉Spring MVC拾取注释,例如@RequestMapping
有关注释驱动的的更多信息,请参阅,有趣的是,这项工作使我的手动请求通过,但我的测试仍然失败。当然,这仍然是一次投票。有什么想法吗?我的测试形式不正确吗?@Vidya测试运行时有任何异常吗?可能尝试提供整个url,而不是
/
无例外;仅java.lang.AssertionError:应为状态:但为:
。完整的URL没有变化。奇怪的但我想这是另一天的问题。感谢您指出context:component scan
是不够的。嗨,凯文,我也有类似的问题,但我已经在我的spring配置中包含了上述标签。你知道为什么它会失败吗?我对SpringMVC是个新手,从在线教程中学习。我在做一项作业时遇到了这个问题。
@Controller
@RequestMapping("/")
public class MyController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model, HttpServletRequest request) {
return "hello";
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("file:src/main/webapp/WEB-INF/dispatcher-servlet.xml")
public class AppIT {
private MockMvc mockMvc;
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
protected WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@Test
public void testMVC() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("hello"));
}
}