spring mvc簡單的demo,springmvcdemo

來源:互聯網
上載者:User

spring mvc簡單的demo,springmvcdemo

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;@Controllerpublic 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>


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.