10
回答
springmvc无法扫描到jar包的@Controller注解
开发十年,就只剩下这套Java开发体系了   

最近在学习maven 想用maven构建项目试试 但是却遇到问题 无法继续

下面是目录结构

view是web层

其他都是以jar包的形式集成在view项目中

这是view项目中的一段能访问的代码

@Controller
public class baseController {
	
	@RequestMapping("sb.do")
	@ResponseBody
	public String sb(){
	
		return "sb";
	}
}




我在common项目中写一点类似的代码 却无法访问 

@Controller
public class BaseController {
	
		@Resource
		IUserCityService cityService;
	
		@RequestMapping("city.do")
		@ResponseBody
		public Object cityByParent(Integer parent){
			if(parent==null){
				parent=0;
			}
			return cityService.selectByParent(parent);
			
		}
		
}




springMvc配置文件

<context:component-scan base-package="com.*"	use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:include-filter type="annotation"
			expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>
	<!-- 会自动注册了 -->
	<mvc:annotation-driven enable-matrix-variables="true" />

	<mvc:default-servlet-handler />

	<mvc:resources mapping="/css/**" location="/css/" />
	<mvc:resources mapping="/js/**" location="/js/" />
	<mvc:resources mapping="/images/**" location="/images/" />



	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"	value="org.springframework.web.servlet.view.JstlView" />
		<property name="contentType" value="text/html" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!--MVC的ResponseBody输出中文编码设定,防止中文乱码 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>



view 项目打包配置pom文件

<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.store</groupId>
		<artifactId>store</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>

	<artifactId>store-view</artifactId>
	<packaging>war</packaging>

	<name>store-view Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<!-- 引用其他模块 -->
		<dependency>
			<groupId>com.store</groupId>
			<artifactId>store-buy</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>com.store</groupId>
			<artifactId>store-common</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>com.store</groupId>
			<artifactId>store-user</artifactId>
			<version>${project.version}</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warName>store</warName>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>




举报
James11
发帖于3年前 10回/11K+阅
顶部