SpringMVC注解可以帮助我们快速地注入 属性和参数 提高开发效率。
由于
有相当一部分人讨厌xml配置方式
注解可以覆盖 xml则不能
使用注解比xml规范化,因为很多注解都是java的规范的范畴,当你使用象jndi,或jpa这样规范化统一框架时不需要更改注解 ,xml则不行
缺点:
不利于维护,springmvc xml配置文件可以看清所有的mvc架构,易于维护,可读性强。
运行的错误:
严重: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: javax/portlet/PortletResponse
上述问题一般就是jar包引错或者是
解决方案:
你bean里是不是引入了一个InternalResourceViewResolver类,这个类的包是web.servlet,不是web.portlet;
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
引入jar包
1.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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!--基础配置有springMVC配置的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*:config/SpringMVCAnnotation-servlet.xml</param-value>
<!--classpath*代表在src下寻找config文件夹再在其中寻找以-servlet.xml文件结尾的文件-->
</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>
web.xml
2.SpringMVCAnnotation-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">
<!-- 注解开始 -->
<!--spring启动时的注解扫描包-->
<context:component-scan base-package="annotation"></context:component-scan>
<!-- 两个bean的作用是根据url找类,根据类去找方法-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!--以上两句就是设置spring的拦截器不对img文件夹与js文件夹的文件进行拦截--> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property> <!-- 前缀 -->
<property name="suffix" value=".jsp"></property> <!-- 后缀 -->
</bean>
</beans>
SpringMVCAnnotation.xml
3.UserController.java
package 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 {
//value指的是浏览器要请求的地址,method指的是请求的方式
@RequestMapping(value="/user/addUser",method=RequestMethod.POST)
public ModelAndView addUser(){
String result ="---1.this is addUser---";
return new ModelAndView("/annotation","result",result);
}
@RequestMapping(value="/user/delUser",method=RequestMethod.GET)
public ModelAndView delUser(){
String result ="---2.this is delUser---";
return new ModelAndView("/annotation","result",result);
}
@RequestMapping(value="/user/toUser",method=RequestMethod.GET)
public ModelAndView toUser(){
String result ="---3.this is toUser---";
return new ModelAndView("/touser","result",result);
}
}
/*此处请注意,@Controller的使用。@RequestMapping(value="/user/addUser",method=RequestMethod.POST)中的value表示跳转路径,method表示通过哪种方式调用这个方法*/
Usercontroller.java
4.annotation.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head> <body>
<h1>SpringMVC注解1 <h1/><br>
${result}
</body>
</html>
annotation.jsp
5.touser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form action="/springMVC4/user/addUser" method="post">
<h1>SpringMVC注解</h1>
<br>
${result }
<input type="submit" value="post请求">
</form>
</body>
</html>
touser.jsp