您的位置:首页 > 编程语言 > Java开发

Spring MVC常见bug总结----持续更新中

2017-07-27 15:18 330 查看
一、

  Spring MVC的配置文件Springmvc-servlet.xml报错,在添加

<context:component-scan base-package="controller" />
  来指定控制器所在的包时,窗口显示红叉,报错内容为:

Multiple annotations found at this line:

    - schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/

     springcontext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root

     element of the document is not <xsd:schema>.

    - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element

     'context:component-scan'.

  经过检查,发现时springmvc-servlet的头中,关于命名空间(是不是这么说的。。。)的定义有误,导致找不到我对应的文件。

  之前,我的头定义如下:

 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/springcontext.xsd">[/code] 
xsd等没有指定正确的文件版本,根据自己的jar包版本做修改如下:

<beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

二、依然是坑爹的配置bug

 我在实验一个简单的Spring MVC的demo的时候,所有的代码和jar、环境均已经搞定,但是run as server之后,出现:

org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 75; cvc-elt.1: 找不到元素 'beans' 的声明
这样的报错。

在实验切xmlns中的文件版本等等方法之后,依然没有效果。但是奇怪的是,我之前一个类似的项目,该配置文件极其类似,只是从传统控制器风格变成了使用注解控制器,程序并没有报错!

后修改,在beans元素前面加上一个命名空间beans,然后就不报错了。

前后对比图如下:

1.发生错误的时候:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<mvc:annotation-driven/>
<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="controller" />
<mvc:resources mapping="/*.html" location="/"/>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>


2.添加beans命名空间之后:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<mvc:annotation-driven />
<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="net.use" />

<!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
<beans:bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<beans:bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<!-- 这个类用于Spring MVC视图解析 -->
<beans:bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/pages/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

</beans:beans>
不报错了。。。

坑爹。。。

到现在还不知道是什么原因。

3.今天准备尝试一下实现依赖注入,途中发生了一个有趣的bug。

严重: Servlet.service() for servlet [s nested exception is org.springframewo
具体的描述没了
主要是怎么回事呢。

说表单数据提交后自动绑定到User类的时候发生了一个错误,无法从String对象转换成自己定义的Connection对象。

恍然想起来,form提交到服务器的全部是String,但是我们自己定义的类的属性可能是double,可能是一个对象。

所以为了解决这个问题,参考了之前看到的某篇文章,在转成User类之前,先换成一个所有属性都用String表示,属性值和User类一一对应的UserForm类。如下:

package net.use.info;

import java.io.Serializable;

import net.use.connection.Connection;

public class User implements Serializable {
/**
* @author zjn
*/
private static final long serialVersionUID = 1L;
private String name; // name
private String pwd; // pwd
private Integer age; // age

private String sex;

private Connection connection;//connection是接口
//全部实现了set和get方法�? 以此完成依赖注入(设值注入)
public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex=sex;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public Connection getConnection() {
return connection;
}

public void setConnection(Connection connection) {
this.connection = connection;
}

}

然后是userForm类
package net.use.form;

public class UserForm {
private String name; // name
private String pwd; // pwd
private Integer age; // age

private String sex;

private String connection;//connection是接口
private String connection_type;
public String getName() {
return name;
}
public void setName(String name) {
this.nam
4000
e = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getConnection() {
return connection;
}
public void setConnection(String connection) {
this.connection = connection;
}
public String getConnection_type() {
return connection_type;
}
public void setConnection_type(String connection_type) {
this.connection_type = connection_type;
}

}


这样,在Controller中,就可以先把Form数据换成UserForm对象,然后再一个一个把属性值赋给User对象。如下:
@RequestMapping("register")
public String Save(@ModelAttribute("form") UserForm userForm, Model model) { // user:视图层传给控制层的表单对象;model:控制层返回给视图层的对�?
//model.addAttribute("user", user);
//System.out.println(user.getName()+","+user.getSex()+","+user.getPwd()+","+user.getAge());

//first,to convert the userform to the user
User user = new User();
user.setName(userForm.getName());
user.setAge(userForm.getAge());
user.setSex(userForm.getSex());
user.setPwd(userForm.getPwd());

Connection con;
if(userForm.getConnection_type().equals("phone")) {
con = new Phone();
con.setNumber(userForm.getConnection());
user.setConnection(con);
}else if(userForm.getConnection_type().equals("msn")) {
con = new MSN();
con.setNumber(userForm.getConnection());
user.setConnection(con);
}

怪不得之前看的那篇文章要用什么Form类,原来是这个鬼。。。。get!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: