Maven Spring 3动态应用程序中的错误

时间:2016-11-02 11:01:27

标签: java maven jsp spring-mvc spring-3

我正在使用mavenspring 3用于dynamic project。我在<bean name="/hello.cs" class="com.maven.controller.HelloController"/>收到错误Multiple annotations found at this line: - Class 'com.maven.controller.HelloController'

项目结构

enter image description here

Web.xml中

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>*.cs</url-pattern>
  </servlet-mapping>
</web-app>

Spring.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <bean name="/hello.cs" class="com.maven.controller.HelloController"/>


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

的index.jsp

<jsp:forward page="WEB-INF/jsp/home.jsp"></jsp:forward>

针对home.jsp

<form action="./hello.cs" method="post">
    <div>
        <div>
            User Name:<input type="text" name="name"/>
        </div>
        <div>
            Password: <input type="password" name="pass"/>
        </div>
        <div>
            <input type="submit" value="Login"/>
        </div>
        <div>
            <p style="color: red">${msg}</p>
        </div>
    </div>
</form>

HelloController中

package com.maven.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController extends AbstractController{

    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        String name = req.getParameter("name");
        String pwd = req.getParameter("pass");

        Map<String, String> m = new HashMap<String, String>();

        if(name.equalsIgnoreCase("xyz") && pwd.equalsIgnoreCase("123")){

            m.put("msg", "welcome mr. "+req.getParameter("name"));
            return new ModelAndView("success", m);
        } else {
            m.put("msg", "Invalid");
            return new ModelAndView("home");
        }
    }
}

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javatpoint</groupId>
  <artifactId>CubeGeneratorWeb</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>CubeGeneratorWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.0.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>


  </dependencies>
  <build>
    <finalName>CubeGeneratorWeb</finalName>
  </build>
</project>

错误: enter image description here

2 个答案:

答案 0 :(得分:1)

编辑:它说未找到&#39; com.maven.controller.HelloController&#39;

我看到您已将 com.maven.controller.HelloController.java 文件放在 src \ main \ resources 文件夹下。

enter image description here

应该在 src \ main \ java文件夹下。所以层次结构应该像

src
|-main
   |-java
      |-com
        |-maven
           |-controller
              |-HelloController.java

原因是如果您使用默认配置,则只有当maven存放在此层次结构 src \ main \ java 下时,maven才会检测到目录结构。

您是否可以右键单击项目并转到属性&gt; Java构建路径&gt;来源并检查构建路径列表中是否存在 src \ main \ java 条目?如果没有添加,请重试。

答案 1 :(得分:0)

经过多次努力,我得到了答案,我在spring.xml文件中出错了。检查解决方案。

我只需要在spring.xml文件中添加以下代码。

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
                "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

之前我使用的是以下代码

<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-3.0.xsd">

对于上面的代码,我需要更改pom.xml文件,我的意思是需要添加更多依赖项。

所以这只是我这个问题的最终spring.xml文件。

spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
            "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean name="/hello.sa" class="com.maven.controller.HelloController"/>

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