我们得到了2页的绝对简单的应用程序。
代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>page2</title>
</head>
<body>
page2
<a href="page1url">page1</a>
</body>
</html>
控制器代码:
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {
@RequestMapping(value ="/page1url")
public String page1( ) {
return "page1";
}
@RequestMapping(value = "/page2url")
public String page2( ) {
return "page2";
}
}
为了使其工作,我们需要注册控制器。 方式1.经典工作正常。代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<bean class="controller.MainController"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
方式2.使用Spring-context 代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="*"/>
<!--<bean class="controller.MainController"/>-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
方式2不起作用。
web.xml是简单的基本代码
问题: 为什么Spring-context变体不起作用以及如何解决它?
P.S。为很多代码道歉
答案 0 :(得分:1)
将此行添加到Spring-context:
<mvc:annotation-driven />
答案 1 :(得分:0)
尝试在Spring-context
文件中添加以下内容:
<mvc:annotation-driven />
<context:component-scan base-package="controller"/>
删除base-package="*"