spring mvc简单的demo

tomcat配置文件:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
	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_3_0.xsd">
  <display-name>hello</display-name>	

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

   <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
     </servlet-mapping>

</web-app>

springmvc配置文件:spring-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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

	<!-- 注解扫描包 -->
	<context:component-scan base-package="com" />

	<!-- 分派器 -->
  	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  	<property name="prefix" value="/" />
  	<property name="suffix" value=".jsp" />
 </bean>

</beans>

实体:User.java

package com.bean;

public class User {

	public String user;
	public String password;

	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

}

控制器:UserManager.java

package com.controler;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bean.User;

@Controller
public class UserManager {

	@RequestMapping("/login")
	public String login1(User user, HttpServletRequest request)
	{
		System.out.println("hhhhhhhhh"+user.getUser()+"/"+user.getPassword());

		request.setAttribute("value1", user.getUser());
		request.setAttribute("value2", user.getPassword());

		return "index";
	}

}

jsp页面:index.jsp

<%@ page language="java" import="java.util.*" contentType="text/html;charset=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">
	-->

	<script type="text/javascript">
	function addUser()
	{
	var form = document.forms[0];
	form.action="/fly/login";
	form.method="post";
	form.submit();
	}
	</script>
  </head>

  <body>

  <%
  		String user = (String)request.getAttribute("value1");
  		String password = (String)request.getAttribute("value2");

  		out.println(user);
  		out.println(password);
   %>

  	<form action="">
  	用户名:<input name="user" type="text" value="dalangge"/>
  	密码:<input name="password" type="password" value="123456"/>
  	<input type="button" value="登录" onclick="addUser()"/>
  	</form>

    This is my JSP page. <br>
  </body>
</html>
时间: 2024-10-29 05:04:34

spring mvc简单的demo的相关文章

[工作必备]spring定时器简单的demo

原文:[工作必备]spring定时器简单的demo 源代码下载地址:http://www.zuidaima.com/share/1586950010391552.htm 本月的最后一天上班季.给大家分享一个工作中经常用到的一个算得上是技术吧.我在网上找了好多方法,都是简单的描述,并没有案例.今天抽时间做出来了一个简单的demo.希望可以帮助大家. 如图,我在这里配置的定时器是每10秒执行一次. 其实很简单,重点是在配置applicationContext.xml里面的cronExpression

Spring MVC简单原理

Spring MVC简单原理 针对有Java Web基础.Spring基础和Spring MVC使用经验者,文章比较简单,权当自己的一个总结和备忘吧. 前言 目前基于Java的web后端,Spring生态应该是比较常见了.虽然现在流行前后端分离,MVC和后端模板渲染越来越少,后端专注向前端提供数据接口.但由于笔者维护着一个老项目,既有JSP技术也有只返回JSON的接口,两者都是基于Spring MVC这一套技术实现的,所以暂且觉得了解一下Spring MVC原理还是有所裨益的. Spring M

spring mvc简单介绍xml版

spring mvc介绍:其实spring mvc就是基于servlet实现的,只不过他讲请求处理的流程分配的更细致而已. spring mvc核心理念的4个组件: 1.DispatcherServlet:负责接受所有的请求,就像普通的servlet一样,此接口只是简单的负责处理接受请求. 2.HandlerMapping:当接受到请求后,由此组件负责解析请求,知道该请求要访问那个具体的Controller(具体的Servlet). 3.HandlerAdaper:负责调用具体的Controll

Spring MVC 简单入门

web.xml 配置: <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的

基于spring mvc的注解DEMO完整例子

弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件.本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上.下面开始贴代码. 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下. web.xml配置: <?xml version="1.0" encoding="UTF-8"?> 

基于注解配置的Spring MVC 简单的HelloWorld实例应用

2.1 问题 使用注解的方式重构helloworld应用案例. 2.2 方案 1. @RequestMapping注解应用 @RequestMapping可以用在类定义和方法定义上,它标明这个类或方法与哪一个客户请求对应.实例代码如下: @RequestMapping("/day01") public class HelloController { @RequestMapping("/hello.form") public String execute() thro

BootStrap DataTables Spring MVC简单增删改查实例

1 <!DOCTYPE html> 2 <%@ page contentType="text/html;charset=gbk" language="java" %> 3 <%@page isELIgnored="false" %> 4 <meta name="viewport" content="width=device-width, initial-scale=1&quo

Spring mvc简单案例

jar包 <!--junit--><dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope></dependency> <!--spring-context--><dependency>

注解配置的Spring MVC

基于注解配置的Spring MVC 简单的HelloWorld实例应用 2.1 问题 使用注解的方式重构helloworld应用案例. 2.2 方案 1. @RequestMapping注解应用 @RequestMapping可以用在类定义和方法定义上,它标明这个类或方法与哪一个客户请求对应.实例代码如下: @RequestMapping("/day01") public class HelloController { @RequestMapping("/hello.form