zoukankan      html  css  js  c++  java
  • springMVC使用注解方式进行页面跳转

    <!--控制层-->
    package cn.org.spartacus.spring;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    @Controller
    @RequestMapping("index")
    public class testController {
      @Autowired 
         private Test test; 
     
     @RequestMapping("helloWorld")
       public String helloWorld() { 
      test.getsys();
             return "success";
          
        }

     public Test getTest() {
      return test;
     }

     public void setTest(Test test) {
      this.test = test;
     } 
     
     
    }

    <!--控制层调用的类-->
    package cn.org.spartacus.spring;

    import org.springframework.stereotype.Service;

    @Service
    public class Test {
    public void getsys(){
     System.out.println("调入成功!");
    }
     
    }

    <!--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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation=" 
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        "> 
      <!-- 把标记了@Controller注解的类转换为bean-->    
         <context:component-scan base-package="cn.org.spartacus.spring" /> 
       <mvc:annotation-driven /> 
     
        <!-- 定义跳转的文件的前后缀 --> 
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
          <property name="prefix" value="/WEB-INF/jsp/" />  <!-- jsp存放的路径-->
            <property name="suffix" value=".jsp" />  <!-- 指定跳转的页面为.jsp格式 -->
        </bean> 
    </beans>

    <!--web.xml文件的配置-->
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name></display-name> 
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
     
      <servlet>
      <servlet-name>dispather</servlet-name>
      <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/applicationContext.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
      <servlet-name>dispather</servlet-name>
      <url-pattern>*.hml</url-pattern>
     </servlet-mapping>
     
      <!---中文乱码过滤器-->
                <filter>
                <filter-name>encodingFilter</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>
                <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
                </init-param>
                </filter>
                <filter-mapping>
                <filter-name>encodingFilter</filter-name>
               <url-pattern>*.hml</url-pattern>
                </filter-mapping> 
    </web-app>


    <!--jsp的调用-->
    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="app/index/helloWorld" method="post">
    <input type="submit" value="提交" />
    </form>
    </body>
    </html>

  • 相关阅读:
    什么是IOC
    spring的作用
    什么是spring框架?
    72
    71
    70
    69
    68
    67
    66
  • 原文地址:https://www.cnblogs.com/qgc88/p/3282317.html
Copyright © 2011-2022 走看看