springmvc 测试项目示例

新建一个 maven项目 

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.myspring</groupId>
  <artifactId>spring_test</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>

  <name>spring_test</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.1.3.RELEASE</spring.version>
  </properties>
<!-- 插件查询位置 -->
    <!-- <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>the nexus repository</name>
            <url>http://192.168.1.80:8081/nexus/content/groups/public</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
    团队集成发布包
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>the team release repository</name>
            <url>http://192.168.1.80:8081/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>the team snapshotRepository</name>
            <url>http://192.168.1.80:8081/nexus/content/repositories/snapshots</url>
            <uniqueVersion>false</uniqueVersion>
        </snapshotRepository>
    </distributionManagement> -->
    
  <dependencies>
  
      <!-- spring -->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-core</artifactId>
                  <version>${spring.version}</version>
              </dependency>
              <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
                  <version>${spring.version}</version>
              </dependency>
              <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-test</artifactId>
              <version>${spring.version}</version>
            </dependency>
              <!-- mysql connector -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.8</version>
            </dependency>
            
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.4</version>
                <scope>runtime</scope>
            </dependency>
            
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.8</version>
    <!--           <scope>test</scope> -->
            </dependency>
            
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
            </dependency>
            
            
      </dependencies>
  
  
  
  
    <!-- 下面这部分直接复制 ,execution 部分可能会报错,要自己手动去写,然后剪切pom文件,粘贴 -->
      <build>
          <outputDirectory>src\main\webapp\WEB-INF\classes</outputDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals><goal>copy-dependencies</goal></goals>
                        <configuration>
                            <outputDirectory>src\main\webapp\WEB-INF\lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins> 
    </build>
  
  
</project>

新建webapp,WEB-INF 文件夹

spring-mvc.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.myspring.spring_test">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--注解方式-->
<mvc:annotation-driven/>

<!-- begin 解决返回json 乱码,需要注册掉 mvc:annotation-driven ;  或者 在requestMapp中加参数 produces ="text/html;charset=UTF-8" -->
<!-- <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/html;charset=UTF-8</value>   
                        </list>   
                    </property>   
                </bean>   
            </list>   
        </property>  
    </bean>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
        <property name="messageConverters"> 
            <list>  
                <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
                    <property name="supportedMediaTypes"> 
                        <list> 
                            <value>text/html; charset=utf-8</value> 
                        </list> 
                    </property> 
                </bean>
            </list>  
        </property> 
    </bean>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">   
        <property name="interceptors">  
            <list>     
            </list>  
        </property>  
    </bean>   -->
<!-- end 乱码 -->


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<!--     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>   -->
    <property name="prefix" value="/"/>  
    <property name="suffix" value=".jsp"/>  
</bean>
<mvc:default-servlet-handler/>


</beans>

 

web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
    version="2.5">
  <display-name>web service</display-name>
  
      
      <!-- for soap ws -->
      <!-- <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
              classpath*:applicationContext.xml
          </param-value>
      </context-param>
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener> -->
      
      
      <filter>
          <filter-name>charEncodingFilter</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>charEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <servlet>
          <servlet-name>springServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springServlet</servlet-name>
          <url-pattern>/</url-pattern><!-- 注意不是/*,否则访问不到页面,报一个红框或者直接在页面 出现 jsp源码-->
      </servlet-mapping>
      
      
      
      

  <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

TestController 类

package com.myspring.spring_test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value="/test")
public class TestController {
    
    @RequestMapping(value="/register",produces = "text/html;charset=UTF-8") //解决返回值乱码
    @ResponseBody
    public String register(@RequestParam("username")String username,@RequestParam("password")String password){
        
        System.out.println(username+":"+password);
        
        return "注册成功";
        
    }
    
    @RequestMapping(value="/toregister")
    public String toregister(){
        
        return "index";
        
    }
    
    
}

index.jsp

<?xml version="1.0" encoding="utf-8" ?>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insert title here</title>
</head>
<body>
    <form action="/test/register" name="f" method="post">
    <input type="text" value="" name="username"/>
    <input type="password" value="" name="password"/>
    <input type="submit" value="dengl"/>
    </form>
</body>
</html>

 

posted on 2015-03-18 09:53  远方的人  阅读(458)  评论(0编辑  收藏  举报

导航