springMVC基本配置

web.xml配置

--------------------------------------------------------------------------------------------------------------------------------------------

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3. xmlns="http://java.sun.com/xml/ns/javaee" 
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  6. id="WebApp_ID" version="2.5">
  7. <display-name>mvc</display-name>
  8. <welcome-file-list>
  9. <welcome-file>index.html</welcome-file>
  10. <welcome-file>index.htm</welcome-file>
  11. <welcome-file>index.jsp</welcome-file>
  12. <welcome-file>default.html</welcome-file>
  13. <welcome-file>default.htm</welcome-file>
  14. <welcome-file>default.jsp</welcome-file>
  15. </welcome-file-list>
  16. <servlet>
  17. <servlet-name>mvcServlet</servlet-name>
  18. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  19. <init-param>
  20. <param-name>contextConfigLocation</param-name>
  21. <param-value>classpath*:config/*.xml</param-value>
  22. </init-param>
  23. <load-on-startup>1</load-on-startup>
  24. </servlet>
  25. <servlet-mapping>
  26. <servlet-name>mvcServlet</servlet-name>
  27. <url-pattern>/</url-pattern>
  28. </servlet-mapping>
  29. </web-app>

--------------------------------------------------------------------------------------------------------------------------------------------



springMVC-servlet.xml

--------------------------------------------------------------------------------------------------------------------------------------------

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:context="http://www.springframework.org/schema/context"    
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"    
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  10.     http://www.springframework.org/schema/tx   
  11.     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  12.     http://www.springframework.org/schema/context  
  13.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  14.     http://www.springframework.org/schema/mvc  
  15.     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  16.     <!--  
  17.    <bean name="/test" class="springMVC.Welcome" /> 
  18.    -->
  19.    <context:component-scan base-package="springMVC"></context:component-scan>
  20.    <mvc:annotation-driven/>
  21.   
  22.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  23.         <property name="prefix" value="/"/>  
  24.         <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->  
  25.     </bean>   
  26. </beans> 

--------------------------------------------------------------------------------------------------------------------------------------------



Welcome.java

--------------------------------------------------------------------------------------------------------------------------------------------

  1. package springMVC;

  2. import javax.servlet.http.HttpServletRequest;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;


  6. @Controller
  7. public class Welcome {
  8. @RequestMapping(value = "/action", method = RequestMethod.POST)
  9. public String welcome(String name,String age, HttpServletRequest req){
  10. System.out.print(name + "----->" + age);
  11. req.setAttribute("name", name);
  12. req.setAttribute("age", age);
  13. return "/welcome";
  14. }
  15. }

--------------------------------------------------------------------------------------------------------------------------------------------


    index.jsp

    --------------------------------------------------------------------------------------------------------------------------------------------

    1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>


    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8.   <head> 
    9.     <title>index.jsp</title>
    10.   </head>


    11.   <body>
    12.    <form name="userForm" method = "post" action = "/test/action" >
    13.      <br/>
    14.      <br/>
    15.      name:<input type="text" id="name" name="name"/>
    16.      <br/>
    17.      <br/>
    18.      age :<input type="text" id="age" name="age"/>
    19.      <br/>
    20.      <br/>
    21.      <input type="submit" value="submit" />
    22.      <br>
    23.      <br>
    24.      <input type="reset"  value="resset" />
    25.      </form>
    26.   </body>


    27. </html>

    --------------------------------------------------------------------------------------------------------------------------------------------


      welcome.jsp

      --------------------------------------------------------------------------------------------------------------------------------------------

      1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      2.     pageEncoding="ISO-8859-1"%>
      3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      4. <html>
      5. <head>
      6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      7. <title>welcome ${name}</title>
      8. </head>
      9. <body>
      10. welcome ${name}
      11. <br/>
      12. your age is ${age}
      13. </body>
      14. </html>

      --------------------------------------------------------------------------------------------------------------------------------------------






        评论
        添加红包

        请填写红包祝福语或标题

        红包个数最小为10个

        红包金额最低5元

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

        抵扣说明:

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

        余额充值