小菜鸟学习SpringMVC(二) 注解开发

SpringMVC注解学习

1.导入jre包


2.web.xlm

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>TestSpringMVC</display-name>


<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
 
 
 <servlet>
 <servlet-name>springmvc</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <!-- 按默认加载springmvc.xml的规范,  就不需要在这里配置
  默认加载规范:
  文件名:servlet-name-servlet=====>springmvc-servlet
  路径规范:必须在WEB-INF下
 -->
<!--  <init-param>
 contextConfigLocation 是DispatcherServlet类中的一个参数 
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:springmvc.xml</param-value>
 </init-param> -->
 </servlet>
 <servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
 <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
</web-app>

3.在WEB-INF下配置springmvc的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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="com.gog"></context:component-scan>


<!-- 处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

<!-- 配置sprigmvc视图解析器:解析逻辑试图 后台返回逻辑试图:index 视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>


</beans>



4.自定义的Controller 这里就不需要在继承或实现类类 ,只需要使用注解来使用、

@Controller
@RequestMapping("my")
public class MyController {


//@RequestMapping(value="/abc.do",method=RequestMethod.POST)  POST无法访问,因为在这里是默认GET请求


//@RequestMapping(value="/abc.do",method=RequestMethod.GET) 
//@RequestMapping(value="/abc.do")  
@RequestMapping("abc")  
public String  Myhello(){

// 返回一个逻辑视图
return "hello";  
}
}


a.在类名前面添加@RequestMapping("/my") 是为了分层思想,自定义根路径  如上面访问Myhello()时就是使用url  /my/abc.do
b.在方法名前添加@RequestMapping()  就是访问这个方法所返回的页面路径 有很多中表达方式
@RequestMapping(value="/abc.do",method=RequestMethod.POST)  POST无法访问,因为在这里是默认GET请求
@RequestMapping(value="/abc.do",method=RequestMethod.GET) 
@RequestMapping(value="/abc.do")  
@RequestMapping("abc")  
...

c.在定义方法的时候,如果是String类型的方法,return返回的就是逻辑视图
d.在定义方法的 时候,如果是void类型的方法,就是不进行页面跳转,单纯的进行页面测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值