java注解springmvc,【SpringMVC】基于注解的SpringMVC简单示例

一、SpringMVC下载

在Spring官网下载Spring Framework

二、在MyEclipcs中新建Web Project,并导入jar包(我导入了过多的jar包,是为了以后扩展其他框架(Spring)准备)。

2b5c12cbd9452506d0b43254ae22b706.png

三、修改web.xml,加入SpringMVC控制中心的servlet配置<?xml version="1.0" encoding="UTF-8"?>

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">

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/springMVC/servlet*.xml

1

SpringMVC

*.do

index.jsp

四、新建controller。我这里定义了两一个资源路径,但是区别了请求方式,如下代码,当请求为get方式时,页面跳转到login视图,我稍后会将login视图映射到login.jsp;当请求为post时,执行登录操作,我这里假设是完成了登录的业务逻辑,跳转到index视图(index.jsp)。@Controller

@RequestMapping("Admin")

public class AdminController {

@RequestMapping(value = "/login", method = RequestMethod.GET)

public ModelAndView login(HttpServletRequest request,

HttpServletResponse response) {

return new ModelAndView("login");

}

@RequestMapping(value = "/login", method = RequestMethod.POST)

public String doLogin() {

return "index";

}

}

五、新建controller配置文件。在WEB-INF下建立一个新文件夹“SpringMVC”,并建立一个xml文件,命名为servlet.xml。<?xml version="1.0" encoding="UTF-8"?>

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:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd">

六、在WEB-INF下建立新文件夹jsp(jsp隐藏),并新建两个jsp文件“index.jsp”,“login.jsp”。

login.jsp中建立一个表单,用于提交post请求。

七、发布程序,启动tomcat。GET请求路径:"http://localhost:8080/MyBlog/Amdin/login.do",则跳转到login.jsp页面,填写好form后提交(POST)到"http://localhost:8080/MyBlog/Amdin/login.do",则进入index.jsp。

八、至此一个简单的基于注解的SpringMVC就搭建好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值