gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:18:07 28 4
gpt4 key购买 nike

我正在使用maven , spring 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

最佳答案

编辑:它说找不到类'com.maven.controller.HelloController'

我发现您已将 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 下时才会检测 java 文件。

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

关于java - Maven Spring 3 动态应用程序中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40378313/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com