Maven 中的错误 Spring 3 动态应用程序
Error In Maven Spring 3 Dynamic Application
我正在使用 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'
收到错误
项目结构
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>
错误:
编辑:它说 class 'com.maven.controller.HelloController' 未找到。
我看到你把你的 com.maven.controller.HelloController.java 文件放在 src\main\resources 文件夹下。
它应该在src\main\java文件夹下。所以层次结构应该像
src
|-main
|-java
|-com
|-maven
|-controller
|-HelloController.java
原因是 Maven 所遵循的目录结构仅当文件位于此层次结构 src\main\java 下时才会检测 java 文件,如果您使用的是默认配置.
您可以右键单击项目并转到 Properties > Java Build Path > Source 并检查是否 src\main\java 条目是否在构建路径列表中?如果没有添加,请重试。
经过多次努力我得到了答案,我在 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>
我正在使用 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'
项目结构
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>
错误:
编辑:它说 class 'com.maven.controller.HelloController' 未找到。
我看到你把你的 com.maven.controller.HelloController.java 文件放在 src\main\resources 文件夹下。
它应该在src\main\java文件夹下。所以层次结构应该像
src
|-main
|-java
|-com
|-maven
|-controller
|-HelloController.java
原因是 Maven 所遵循的目录结构仅当文件位于此层次结构 src\main\java 下时才会检测 java 文件,如果您使用的是默认配置.
您可以右键单击项目并转到 Properties > Java Build Path > Source 并检查是否 src\main\java 条目是否在构建路径列表中?如果没有添加,请重试。
经过多次努力我得到了答案,我在 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>