MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

2025-11-11 00:10:13

1、创建一个maven项目如图:

MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

2、项目创建完毕后整体架构如图:

MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

3、配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>website</display-name>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>*.json</url-pattern>

</servlet-mapping>

<filter>

<filter-name>CharacterEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>utf-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

  <welcome-file-list>

    <welcome-file>WEB-INF/jsp/MyJsp.jsp</welcome-file>

  </welcome-file-list>

</web-app>

MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

4、配置dispatcher-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans  xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" default-autowire="byName"

xmlns="http://www.springframework.org/schema/beans"

xsi:schemaLocation="

      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd       

      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd       

 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:annotation-config/>

<context:component-scan base-package="com"> 

</context:component-scan> 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >

<property name="messageConverters">  

        <ref bean="mappingJacksonHttpMessageConverter"/>  

    </property>  

</bean>    

     <bean id="mappingJacksonHttpMessageConverter" 

     class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  

   <property name="supportedMediaTypes">  

       <list>  

           <value>text/html;charset=UTF-8</value>  

       </list>  

   </property>  

</bean>  

<!-- ViewResolver -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="requestContextAttribute" value="rc" />

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

<property name="prefix" value="/WEB-INF/jsp/" />

<property name="suffix" value=".jsp" />

</bean>

</beans>

MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

5、配置applicationContext.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:aop="http://www.springframework.org/schema/aop"  

    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  

    xmlns:context="http://www.springframework.org/schema/context"  

    xsi:schemaLocation="  

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  

     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  

     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  

     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">

<context:annotation-config/>

<context:component-scan base-package="com">

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 

</context:component-scan> 

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

<import resource="conf/spring/*.xml"/>

</beans>

MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

6、写一个程序跑一下项目结果如下,大功告成:

MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

MyEclipse10搭建MavenSpringMVC注解JavaWeb项目

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢