软件工程综合实践(2)
LoginAction 到底做了什么?
1. 通过String username = request.getParameter("username");
获取了页面当中输入的用户名
2. 有可能获取到乱码,那可以通过
username = new String (username.getBytes("ISO-8859-1"),"utf-8");
转码 如果 获取的信息不是乱码,那你就不要转码了,否则会转换成乱码
3. 判断一下是否能登陆(获取的用户名和密码是否都匹配)
if("neusoft".equals(username)&&"123".equals(pwd))
4. WEB-INF下的jsp页面不能直接跳转,需要通过
request.getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request, response);
转发,才能够跳转
5. request.getRequestDispatcher 可以携带 request.setAttribute的信息
6. Request转发之后的页面,可以通过el表达式获取setAttribute的信息
${uname } 注意 uname 是
request.setAttribute("uname", username);这个方法中的uname
7. Response 是 重定向,不能携带数据
8. Session里面的数据 response 和 request 都能传递
<%@ page language="java" import="java.util.*" pageEncoding="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 pagetitle> <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"> head> <body> <a href="login.jsp">登陆a> body> html>
<%@ page language="java" import="java.util.*" pageEncoding="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 'login.jsp' starting pagetitle> <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"> head> <body> <form action="LoginAction" method="get"> 用户名:<input type="text" name="username" id="username" placeholder="请输入账户" required="required"/> <font color="red">${unameErr }font> <br/> 密 码:<input type="password" name="pwd" id="pwd" placeholder="请输入密码" required="required"/> <font color="red">${pwdErr }font> <br/> <input type="submit" value="登录"/> form> body> html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% 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 'users.jsp' starting pagetitle> <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"> head> <body> <center> <h1>用户列表h1> <table border="1"> <tr> <td>用户编号td> <td>用户名td> <td>密码td> tr> <c:forEach items="${users }" var="d"> <tr> <td><a href="#">${d.userid }a>td> <td>${d.username }td> <td>${d.pwd }td> tr> c:forEach> table> center> body> html>
xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <display-name>display-name> <servlet> <servlet-name>springmvcservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:springmvc.xmlparam-value> init-param> servlet> <servlet-mapping> <servlet-name>springmvcservlet-name> <url-pattern>*.actionurl-pattern> servlet-mapping> <welcome-file-list> <welcome-file>index.jspwelcome-file> welcome-file-list> web-app>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <bean id="UsersController1" name="/users.action" class="cn.neusoft.controller.UsersController1">bean> <context:component-scan base-package="cn.neusoft.controller.UsersController">context:component-scan> <mvc:anotation-driven>mvc:anotation-driven> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">bean> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">bean> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter ">bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp">property> <property name="suffix" value=".jsp">property> bean> beans>
在Mybatis的学习之后,我们学习了前端代码,Javaservlet和springmvc,在写代码之前首先要将"ISO-8859-1"修改为"utf-8",否则会出现乱码。以及,一开始无法连接服务器时,要进行Tomcat的配置。
springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合。springmvc是一个基于mvc的web框架。
Spring为展现层提供的基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一
Spring3.0后全面超越Struts2,成为最优秀的MVC框架
SpringMVC通过一套MVC注解,让POJO成为处理请求的控制器,而无须实现任何接口
支持REST风格的URL请求
采用了松散耦合可插拔组件结构,比其他MVC框架更具扩展性和灵活性