springMVC注解及优化

Posted wujixing909

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springMVC注解及优化相关的知识,希望对你有一定的参考价值。

1. 新建web project

2. 添加jar

3. 改写web.xml, 注意spring-servlet.xml的名字 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

 4. 写 springAnnotation-servlet.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"> 
    <!-- 注解扫描包 -->
    <context:component-scan base-package="com.tgb.web.controller.annotation" />        
    <!-- 开启注解 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
    
    <!-- 静态资源访问 -->
    <mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
   
    	
    <!-- ViewResolver 视图解析器 --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean>
     
</beans>

 5. 新建包 com.tgb.web.controller.annotation, 新建类userController.java

    注意加注解的方式, 类注解和方法注解

package com.tgb.web.controller.annotation;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {
	@RequestMapping(value="/user/addUser", method=RequestMethod.GET)
	public ModelAndView addUser(){
		String result = "this is addUser---------";
		return new ModelAndView("/annotation","result",result); 
	}
	@RequestMapping(value="/user/delUser", method=RequestMethod.GET)
	public ModelAndView delUser(){
		String result = "this is delUser---------";
		return new ModelAndView("/annotation","result",result); 
	}
}

 6. 根据spring-servlet.xml设置, 在/WEB-INF/jsp/下新建  annotation.jsp  

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP ‘annotation.jsp‘ starting page</title>
  </head>  
  <body>	
	  <h> annotation 注解1 </h> <br>
	  <div>
	      	${result }
	  </div>
  </body>
</html>

 

7. IE端访问:     

http://localhost:8080/springMVC3/user/addUser

 

显示结果: 

annotation 注解1  
this is addUser--------- 

  

  

  

以上是关于springMVC注解及优化的主要内容,如果未能解决你的问题,请参考以下文章

Spring基于注解及SpringMVC

java学习--SpringMVC使用及注解

SpringMVC常用注解及参数绑定(02)

SpringMVC常用注解及参数绑定(02)

SpringMVC原理及非注解配置详解

Spring之手写SpringMVC5个注解(之IOC,DI优化)了解三级缓存