Java 在 spring 中使用注释时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21985283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:50:31  来源:igfitidea点击:

getting errors while using annotations in spring

javaspring

提问by user3345514

i am getting errors when using @Requestmappingin LoginController.java.

使用@Requestmappingin时出现错误LoginController.java

Getting errors like

得到像这样的错误

Multiple markers at this line
    - The attribute value is undefined for the annotation type 
     RequestMapping
    - RequestMethod cannot be resolved to a variable
    - The attribute method is undefined for the annotation type 
     RequestMapping
    - RequestMapping cannot be resolved to a type and some other errors also.
Please check bean.xml file also.

LoginController.java

登录控制器.java

package control;
import model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
@Controller
public class LoginControl{
private ApplicationContext context = null;
private UserJDBCTemplate userJDBCTemplate = null;
public LoginControl(){
    context = new ClassPathXmlApplicationContext("Beans.xml");
    userJDBCTemplate = (UserJDBCTemplate)context.getBean("userJDBCTemplate");
} 

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView userLogin() {
    return new ModelAndView("login", "command", new User());
}

@RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
public String checkUser(@ModelAttribute("SpringWeb")User user, ModelMap model) {
      model.addAttribute("username", user.getUsername());                    
      if(userJDBCTemplate.checkLogin(user)){
          return "loginsuccess";
      }          
      return "loginerror";
}   
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView userAdd() {
    return new ModelAndView("add", "command", new User());
}

@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addUser(@ModelAttribute("SpringWeb")User user, ModelMap model) {
      model.addAttribute("username", user.getUsername());                    
      if(userJDBCTemplate.create(user)){
          return "addsuccess";
      }         
      return "adderror";
}
}

beans.xml

bean.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-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/> 
<context:component-scan base-package="control"/>
<bean id="dataSource"                                                                                       
   class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/login"/>
  <property name="username" value="root"/>
  <property name="password" value="infoobjects"/>
</bean>

<!-- Definition for userJDBCTemplate bean -->
<bean id="userJDBCTemplate"   class="control.UserJDBCTemplate">
  <property name="dataSource"  ref="dataSource" />   
</bean>      
</beans>

采纳答案by Abhishek Nayak

check your classpath for spring-webmvc.jarfile is available or not, Assuming you using a eclipse, then to conform a class is available or not in classpath. click ctrl+shift+t, A OpenType window will open, in the typename type RequestMapping. if it show's in Matching items then it's available else not. In your case it's not that's why you getting error

检查您的类路径中的spring-webmvc.jar文件是否可用,假设您使用的是 Eclipse,那么为了使类在classpath. 单击ctrl+ shift+t,将打开一个 OpenType 窗口,在 typename 中键入 RequestMapping。如果它显示在匹配项目中,则它可用,否则不可用。在您的情况下,这不是您出错的原因

The attribute value is undefined for the annotation type

Secondly, To get a bean from container there are many ways like

其次,要从容器中获取 bean,有很多方法,例如

  1. By implementing BeanFactoryinterface
  2. By implementing ApplicationContextAwareinterface
  1. 通过实现BeanFactory接口
  2. 通过实现ApplicationContextAware接口

for example to get bean by using ApplicationContextAwareinterface it provides a setApplicationContext()to get access context.

例如通过使用ApplicationContextAware接口获取bean,它提供了一个setApplicationContext()获取访问上下文。

public class LoginController implements ApplicationContextAware {

  private ApplicationContext applicationContext;
  private UserJDBCTemplate userJDBCTemplate;

  public String checkUser(){

     //get your UserJDBCTemplate bean from container and use it
     userJDBCTemplate = (UserJDBCTemplate)context.getBean("userJDBCTemplate");
  }

  //Here, spring will set ApplicationContext
  void setApplicationContext(ApplicationContext applicationContext) {
    this.applicationContext = applicationContext;
  }
}

But these are bad ways, cause are not Inversion of Control(IoC). you should use spring's iversion control.

但这些都是不好的方式,原因不是控制反转(IoC)。您应该使用 spring 的反转控制。

In your bean configuration file, you have declared <context:annotation-config/>, But you not using this tag functionality, which is used for activating various annotations in bean classes like @Required, @Autowired, @PostConstruct, @PreDestroyand @Resource(if available).

在你的bean的配置文件,你已经宣布<context:annotation-config/>,但你不使用这个标记功能,该功能用于在bean类激活各种注解一样@Required@Autowired@PostConstruct@PreDestroy@Resource(如果可用)。

So in your case you should use @Autowiredannotaion to inject UserJDBCTemplatebean

所以在你的情况下你应该使用@Autowiredannotaion 来注入UserJDBCTemplatebean

to use IoC you have to modify your beans configuration file like @freakman said in his answer.

要使用 IoC,您必须像@freakman 在他的回答中所说的那样修改您的 bean 配置文件。

add <mvc:annotation-driven/>

添加 <mvc:annotation-driven/>

Note:To use above tag, add this schema to top of your beans configuration file.

注意:要使用上面的标记,请将此架构添加到 bean 配置文件的顶部。

xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc

change your LoginControllerclass constructor to use constructor injection like:

更改您的LoginController类构造函数以使用构造函数注入,例如:

@Autowired
public LoginControl(UserJDBCTemplate userJDBCTemplate){
    this.userJDBCTemplate = userJDBCTemplate;
}

Here Spring will automatically provide you UserJDBCTemplatebean to use.

这里 Spring 会自动为你提供UserJDBCTemplatebean 来使用。

回答by freakman

you need to add spring-web dependency to your pom.xml/classpath.

您需要将 spring-web 依赖项添加到pom.xml/classpath.

You will also have to add < mvc:annotation-driven/>to your beans.xmlfile.

您还必须添加< mvc:annotation-driven/>到您的beans.xml文件中。

回答by alok

add spring-web jars on build path if you are not using pom.xml. Download all the jars http://www.javatpoint.com/src/sp/springjars.zip

如果您不使用 pom.xml,请在构建路径上添加 spring-web jar。下载所有的 jars http://www.javatpoint.com/src/sp/springjars.zip

And if you are using pom.xml then add spring-web dependency.

如果您使用的是 pom.xml,则添加 spring-web 依赖项。