注解和XML配置方式同时存在时,注解失效
前面使用了XML配置的方式进行springmvc的测试,一切正常。
后面使用注解做另一个测试,开启了组件扫描。注解配置正常。但是请求路径没办法被注解捕获。
后面把之前xml方式配置的内容注释掉了,请求就可以正常捕获了。
请老师解释下原因。

0
收起
正在回答
4回答
在配置SpringMVC的访问路径时,如果在SpringMVC的配置文件中通过bean的方式配置访问路径,又同时使用注解,会使得程序的运行结果存在问题;
与这个问题类似的问题:我们在学习Servlet时,如果既使用配置文件又使用注解声明Servlet,那么程序也是会出错的。
祝学习愉快!
程序人生001
2018-08-06 15:59:37
配置文件:mySpringmvc.xml
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.imoooc.controller"/> <!--<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">--> <!--<property name="mappings">--> <!--<props>--> <!--<!–<prop key="/login.do">myHandler</prop>–>--> <!--</props>--> <!--</property>--> <!--</bean>--> <bean id="myHandler" class="User"></bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
前端页面:weight.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2018/8/5 0005 Time: 下午 23:26 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>BMI计算器</title> </head> <body> <b>计算BMI身体指数</b> <form action="bmi.do" method="post"> 身高: <input type="text" name="height">cm 体重: <input type="text" name="weight">kg <input type="submit" value="提交测试"> </form> </body> </html>
showBMI.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/8/5 0005
Time: 下午 23:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>BMI结果</title>
</head>
<body>
提示信息:${msg}
</body>
</html>web.xml
<?xml version="1.0" encoding="utf-8" ?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>CharacterEncoding</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>MySpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--指定配置springmvc.xml文件的路径--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mySpringmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>MySpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
实体类:Body
package com.imoooc.controller;
public class Body {
private double height;
private double weight;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}handler:BMIController
package com.imoooc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BMIController {
@RequestMapping("/bmi.do")
public ModelAndView bmiTest(Body body){
ModelAndView modelAndView = new ModelAndView();
double height = body.getHeight();
double weight = body.getWeight();
System.out.println(height+"-----"+weight);
double result = weight/(Math.pow(height/100,2));
System.out.println(height/100);
System.out.println(result);
String msg = "无fuck可说";
if(result<19){
msg="多吃点,草";
}else if(result<25){
msg="你很棒棒哦";
}else{
msg="胖子";
}
modelAndView.addObject("msg",msg);
modelAndView.setViewName("showBMI");
return modelAndView;
}
}POM文件没有什么特别的,引入相关jar包就行
SSM主流框架入门与综合项目实战2018版
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星