spring mvc 完整实例

1.新建工程,把spring所需的包copy到工程里

2.配置前端控制器Dispatcher
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">
<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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

3.在WEB-INF下创建一个spring mvc的xml配置文件,servletname(springmvc)-servlet.xml(命名规则)
配置handlermapping(可以省略此步)
springmvc-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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping" />

<bean name="hello.do" class="zou.huiying.HelloController"></bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>


4.创建jsp页面
hello.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>

<html>
<head>
<title>this is helloworld</title>
</head>
<body>
<form action="hello.do" method="post">
hello:<input type="text" name="hello" />
<input type="submit" value="tijiao" />
</form>
</body>
</html>


5,创建controller
HelloController.java
package zou.huiying;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController {

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String hello=request.getParameter("hello");
System.out.println("---------"+hello);
ModelAndView mav= new ModelAndView("aa");
mav.addObject("helloworld", "nihao"+hello);
System.out.println("---------");
return mav;
// return new ModelAndView(new RedirectView(this.getViewPage()));

}

}


6.指定返回页面
aa.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
This is my JSP page. <br>
<h1>${helloword}</h1>
</body>
</html>


7.配置视图解析器
8.配置controller
上面已经配置好
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC(Model-View-Controller)是一种基于Java的Web应用程序开发框架,它将应用程序分为模型、视图和控制器三个部分,以实现松耦合的设计。下面我将为您介绍一些Spring MVC入门实例编程。 1. 搭建Spring MVC环境 首先,您需要搭建一个Spring MVC环境。您可以使用Maven或Gradle构建工具来创建一个新的Spring项目,然后在pom.xml或build.gradle文件中添加Spring MVC依赖项。您还需要配置一个Servlet容器,如Tomcat或Jetty,以运行您的应用程序。完成这些步骤后,您就可以开始编写Spring MVC应用程序。 2. 编写控制器 控制器是Spring MVC应用程序的核心部分,它负责处理请求并生成响应。要创建一个控制器,请创建一个Java类并注释它为@Controller。然后,您可以使用@RequestMapping注释来定义处理请求的方法。例如,以下代码定义了一个处理GET请求的方法: ``` @Controller @RequestMapping("/hello") public class HelloWorldController { @RequestMapping(method = RequestMethod.GET) public String sayHello(ModelMap model) { model.addAttribute("message", "Hello World!"); return "hello"; } } ``` 在上面的代码中,我们使用@RequestMapping注释将控制器映射到/hello路径。当GET请求发送到该路径时,Spring MVC将调用sayHello()方法。该方法将一个名为“message”的属性添加到模型中,并返回一个名为“hello”的视图名称。 3. 创建视图 视图是控制器生成的响应的一部分,它定义了在浏览器中显示的内容。要创建一个视图,请创建一个JSP文件并将其放在WEB-INF/views目录下。然后,您可以使用Spring标签库来访问模型中的属性。例如,以下代码显示了如何在JSP文件中访问名为“message”的属性: ``` <html> <head> <title>Hello World</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 在上面的代码中,我们使用JSTL表达式${message}来显示模型中的“message”属性。 4. 运行应用程序 完成上述步骤后,您可以使用Servlet容器启动您的应用程序,并在浏览器中访问/hello路径。如果一切顺利,您应该会看到“Hello World”消息显示在浏览器中。 这些是Spring MVC入门实例编程的基本步骤。您可以深入学习Spring MVC,并使用更复杂的功能来构建更高级的Web应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值