spring4mvc

    第一个springmvc demo:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>spring0716</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


   <servlet>
  <servlet-name>SpringDispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springContext.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>SpringDispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- spring的字符集过滤器 -->  
    <filter>  
        <filter-name>Spring character encoding filter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>Spring character encoding filter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>
</web-app>


springContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        ">
        
     <!-- 启动自动扫描该包下所有的Bean(例如@Controller) -->
    <context:component-scan base-package="jiang.gang.li"/>
<!--Spring3.1开始的注解 HandlerMapping -->  
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  

<!--Spring3.1开始的注解 HandlerAdapter -->  
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 

 <!-- 定义视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

  
   
</beans>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>spring test</title>
</head>
<body>
   <form action="springtest.do" method="post">
       <input type="text" name="firstName"/>
       <input type="submit" value="submit"/>
   </form>
</body>
</html>



<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
second.jsp </br>

账户信息<br>
卡号:${persons.cardNo }<br>
密码:${persons.password }<br>
钱数:${persons.balance }<br>
${jianggangli }<br>
</body>
</html>



package jiang.gang.li;


public class Account {
private String cardNo;
private String password;
private float balance;
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}

}



package jiang.gang.li;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


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


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;




@Controller
public class Spring4mvc {


@RequestMapping(method=RequestMethod.POST, value="/springtest.do")
public ModelAndView transValue(HttpServletRequest request){
        Map<String, Object> hashMap = new HashMap<String, Object>();
        String cardNo="cardNo";
String password="password";
Account account1 =new Account();
account1.setCardNo(cardNo);
account1.setPassword(password);
account1.setBalance(12121212.000f);
        hashMap.put("persons", account1);
        hashMap.put("jianggangli", "啊啊啊啊");
        
        ModelAndView modelAndView = new ModelAndView("second", hashMap);
        System.out.println("method=RequestMethod.POST");
        return modelAndView;
    }

@RequestMapping(method=RequestMethod.GET, value="/springtest.do")
public ModelAndView transValue2(HttpServletRequest request){


        System.out.println("method=RequestMethod.GET");
        return new ModelAndView("second");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值