SpringMVC入门教程及其原理讲解
来源:互联网 发布:通达信软件怎么样 编辑:程序博客网 时间:2024/04/29 09:31
package com.pb.entity;
02 03public class Account {
04private String cardNo;
05private String password;
06private float balance;
07public String getCardNo() {
08return cardNo;
09}
10public void setCardNo(String cardNo) {
11this.cardNo = cardNo;
12}
13public String getPassword() {
14return password;
15}
16public void setPassword(String password) {
17this.password = password;
18}
19public float getBalance() {
20return balance;
21}
22public void setBalance(float balance) {
23this.balance = balance;
24}
25 26}
LoginController类:
01package com.pb.web.controller;
02 03import java.util.HashMap;
04import java.util.Map;
05 06import javax.servlet.http.HttpServletRequest;
07import javax.servlet.http.HttpServletResponse;
08 09import org.springframework.web.servlet.ModelAndView;
10import org.springframework.web.servlet.mvc.AbstractController;
11 12import com.pb.entity.Account;
13 14public class LoginController extends AbstractController {
15private String successView;
16private String failView;//这两个参数是返回值传给applicationContext.xml,进行页面分配
17 18public String getSuccessView() {
19return successView;
20}
21public void setSuccessView(String successView) {
22this.successView = successView;
23}
24public String getFailView() {
25return failView;
26}
27public void setFailView(String failView) {
28this.failView = failView;
29}
30@Override
31protected ModelAndView handleRequestInternal(HttpServletRequest request,
32HttpServletResponse response) throws Exception {
33// TODO Auto-generated method stub
34String cardNo=request.getParameter("cardNo");
35String password=request.getParameter("password");
36Account account =getAccount(cardNo,password);
37Map<String ,Object> model=new HashMap<String,Object>();
38if(account !=null){
39model.put("account", account);
40return new ModelAndView(getSuccessView(),model);
41}else{
42model.put("error", "卡号和密码不正确");
43return new ModelAndView(getFailView(),model);
44}
45}//本应该这个方法写在模型层,这地方直接给放在了逻辑层这个地方偷懒了。
46public Account getAccount(String cardNo,String password){
47if(cardNo.equals("123")&&password.equals("123")){
48Account account =new Account();
49account.setCardNo(cardNo);
50account.setBalance(88.8f);
51return account;
52}else{
53return null;
54}
55}
56 57}
applicationContext.xml
01<?xml version="1.0" encoding="UTF-8"?>
02 03<beans xmlns="http://www.springframework.org/schema/beans"
04xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05xmlns:aop="http://www.springframework.org/schema/aop"
06xmlns:tx="http://www.springframework.org/schema/tx"
07xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
08http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
09http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
10<bean id="loginController" class="com.pb.web.controller.LoginController">
11<property name="successView" value="showAccount"></property>
12<property name="failView" value="login"></property>
13</bean>
14<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
15<property name="mappings">
16<props>
17<prop key="/login.do">loginController</prop>
18</props>
19</property>
20</bean>
21<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22<property name="prefix" value="/"></property>
23<property name="suffix" value=".jsp"></property>
24</bean>
25</beans>
Jsp页面:
01<%@ page language="java" contentType="text/html; charset=GB18030"
02pageEncoding="GB18030"%>
03<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04<html>
05<head>
06<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
07<title>Insert title here</title>
08</head>
09<body>
10<a href="login.jsp">进入</a>
11 12</body>
13</html>
login.jsp
01<%@ page language="java" contentType="text/html; charset=GB18030"
02pageEncoding="GB18030"%>
03<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04<html>
05<head>
06<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
07<title>Insert title here</title>
08</head>
09<body>
10${error }
11<form action="login.do" method="post">
12账号登陆<br>
13<hr>
14卡号:<input type="text" name="cardNo"><br>
15密码:<input type="text" name="password"><br>
16<input type="submit" value="登陆">
17</form>
18 19</body>
20</html>
showAccount.jsp
01<%@ page language="java" contentType="text/html; charset=GB18030"
02pageEncoding="GB18030"%>
03<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04<html>
05<head>
06<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
07<title>Insert title here</title>
08</head>
09<body>
10账户信息<br>
11卡号:${account.cardNo }<br>
12密码:${account.password }<br>
13钱数:${account.balance }<br>
14</body>
15</html>
Web.xml
01<?xml version="1.0" encoding="UTF-8"?>
02<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03xmlns="http://java.sun.com/xml/ns/j2ee"
04xmlns:javaee="http://java.sun.com/xml/ns/javaee"
05xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
06<welcome-file-list>
07<welcome-file>index.jsp</welcome-file>
08</welcome-file-list>
09<servlet>
10<servlet-name>Dispatcher</servlet-name>
11<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
12<init-param>
13<param-name>contextConfigLocation</param-name>
14<param-value>classpath:applicationContext.xml</param-value>
15</init-param>
16</servlet>
17<servlet-mapping>
18<servlet-name>Dispatcher</servlet-name>
19<url-pattern>*.do</url-pattern>
20</servlet-mapping>
21</web-app>
SpringMVC总结
以上就是我理解的Spring MVC可能不够深刻。其实对于任何的框架来说,一个框架是一个可复用设计,框架的最大的好处就是复用。每个框架都有存在的理由,那Spring MVC的理由是什么呢?
只有各个框架之间彼此了解他们之间的优缺点,使用场合,使用原理,才能让我们的更快的成长。
0 0
- SpringMVC入门教程及其原理讲解
- SpringMVC入门教程及其原理讲解和实例代码下载
- SpringMVC入门教程及其原理讲解和实例代码下载
- 基于springmvc的文件上传及其讲解
- springmvc+spring jdbc+velocity入门教程及其代码实例
- SpringMVC自定义配置Log4j及其工作原理
- SpringMVC自定义配置Log4j及其工作原理
- SpringMVC入门教程
- SpringMVC 入门教程
- springmvc入门教程
- SpringMVC入门教程
- SpringMVC入门教程
- C#实现P2P之UDP穿透NAT及其原理讲解
- springmvc讲解
- GBDT 入门教程之原理、所解决的问题、应用场景讲解
- SpringMVC深入浅出入门教程
- 编译原理学习周入门教程--(5)上下文无关文法,及其语法树
- Springmvc入门教程,结合Ajax实现最佳精简demo,直击原理,方便易用。
- IOS用CGContextRef画各种图形
- TestNG 入门
- 好玩的linux命令
- hdu 1709 母函数
- Xcode中添加图片文件报错误解决办法:Multiple errors occurred ...
- SpringMVC入门教程及其原理讲解
- objective-c学习之字符串!
- 实现activity全屏显示
- js获取页面加载过程做一个简单的loading
- ssh链接centos服务器速度过慢问题收集
- WebService学习之旅(七)Axis2发布WebService的几种方式
- jar5
- iOS开发中@selector的理解与应用
- mybatis 错误