Spring MVC笔记

MVC入门,使用注解。会了以后超简单,但是在helloworld成功之前,总是会遇到这样那样的问题。

首先,web.xml配置一个servlet

<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/SpringMVC.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.go</url-pattern>
</servlet-mapping>


其次,根据web.xml配置的mvc配置文件建立对应的SpringMVC.xml,并配置如下
(小插曲:这个路径的配置给了我很多麻烦,由于我参考的是一个mvn项目,他的配置是classpath:*/SpringMVC.xml,而mvn项目的mvc配置放在resources目录下,mvn发布的时候会把resources目录下的配置文件拷贝到classes目录下,classpath其实对应的就是web的classes目录,所以导致我的helloworld没有成功加载配置。又由于我使用了*,所以没有报配置文件没找到的错误。web项目成功启动,但是就是找不到mapping。而网上找不到mapping的情况都是url-pattern配的/*导致的。所以配置的时候一定要仔细,有些小错误很难被发现)

<?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:jee="http://www.springframework.org/schema/jee" 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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<context:annotation-config/>
<context:component-scan base-package="com.amhuman.action" />

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="i18n/messages" />
</beans>


接下来就简单了,只要在com.amhuman.action包下建action就可以了(在springMvc中,应该叫控制器):

package com.amhuman.action;

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.ResponseBody;

@Controller
@RequestMapping("/TestAction")
public class TestAction {

@RequestMapping(value = "/test.go", method = { RequestMethod.GET, RequestMethod.POST })
public @ResponseBody String test(){
return "Hello World";
}
}


使用注解的方式进行RequestMapping,并且为了测试简单,以@ResponseBody注解直接返回方法的值,而不是一个view(这种方式适用于ajax请求)。

最后,在index.jsp中添加如下一个form

<form action="TestAction/test.go">
<input type="submit" value="Submit" />
</form>


当然也可以直接访问或者通过ajax请求:http://localhost:8080/JustForFun/TestAction/test.go
将得到返回的"Hello World".
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值