springMVC+Hibernate+jpa整合(2)

最後一步添加springMVCjava

  1. 首先加入springweb文件下的兩個包 springMVC springMVC依賴包 JSON支持 json支持依賴包git

  2. 配置web.xmlgithub

    <!-- lang: xml -->
            <display-name></display-name>	
      	<!-- load spring context -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:spring/applicationContext.xml</param-value>
    	</context-param>
    
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<!-- end -->
    
    	<!-- encodeing filter -->
    	<filter>
            <filter-name>encoding-filter</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>encoding-filter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    	<!-- end -->
    
    	<!-- spring mvc -->
    	<servlet>
    	  	<servlet-name>springMVC</servlet-name>
    	  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
    	  	<!-- 配置springMVC配置文件的路徑 -->
    	  	<init-param>
    	  			<param-name>contextConfigLocation</param-name>
    	  			<param-value>classpath*:springMVC/spring-servlet.xml</param-value>
    	  	</init-param>
    
    	  	<!-- 配置springMVC在tomcat啓動時啓動 -->
    	  	<load-on-startup>1</load-on-startup>
    	</servlet>
      	<servlet-mapping>
      		<servlet-name>springMVC</servlet-name>
      		<url-pattern>/</url-pattern>
      	</servlet-mapping>
    	<!-- end -->
    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
  3. 配置spring-servlet.xmlweb

    <!-- lang: xml -->spring

    <!-- 註解驅動器 -->
    	<mvc:annotation-driven/>
    
    	<!-- 註解掃描器 -->
    	<context:component-scan base-package="com.wyk.sh4.controller"/>
    
    
    	<!-- 靜態文件過濾 -->
    	<mvc:resources location="/img/" mapping="/img/**"/>  
    
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/"></property>
    		<property name="suffix" value=".jsp"></property>
    	</bean>
  4. 添加Controllerjson

    <!-- lang: java -->tomcat

    @Controller
    @RequestMapping("/test")
    public class HelloController { 
        @RequestMapping("/helloWorld")
        public String helloWorld(){
    	    return "helloworld";
        }
    }

github地址:https://github.com/w-cloud-k/DemoSSHmvc

相關文章
相關標籤/搜索