0

i am getting errors when using @Requestmapping in 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

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

<?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>
3
  • 1
    you should implement ApplicationContextAware to get a bean from container. Commented Feb 25, 2014 at 6:09
  • ApplicationContextAware is one option and putting the applicationContext files in context-param which will make them available everytime as we have listeners. Commented Feb 25, 2014 at 6:17
  • actually i am new to spring so can you give me an example how to implement ApplicationContextAware. Commented Feb 25, 2014 at 6:45

3 Answers 3

2

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

check your classpath for spring-webmvc.jar file 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

The attribute value is undefined for the annotation type

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

  1. By implementing BeanFactory interface
  2. By implementing ApplicationContextAware interface

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

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.

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, @PreDestroy and @Resource (if available).

So in your case you should use @Autowired annotaion to inject UserJDBCTemplate bean

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

add <mvc:annotation-driven/>

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

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

change your LoginController class constructor to use constructor injection like:

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

Here Spring will automatically provide you UserJDBCTemplate bean to use.

1 Comment

@user3345514 what error u getting post it to your question.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.