1. 程式人生 > >軟件工程綜合實踐(2)

軟件工程綜合實踐(2)

mvc 配置 spring sun use expires code red 設計理念

軟件工程綜合實踐(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 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"> --> </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 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">
    -->

  </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 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">
    -->

  </head>
  
  <body>
    <center>
        <h1>用戶列表</h1>
        <table border="1">
            <tr>
                <td>用戶編號</td>
                <td>用戶名</td>
                <td>密碼</td>
            </tr>
            <!-- 第二行開始要作信息顯示了,信息比較多,我們使用循環 -->
            <c:forEach items="${users }" var="d">
            <tr>    <!-- ${d.userid }    ,後面的userid    是
                    Userinfo 實體類中 屬性的名字,這裏 必須完全對應,大小寫完全一致
                 -->
                <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>
  
  <!-- springmvc 前端控制器配置 -->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- springmvc.xml 具體進行配置,這裏我們只是讀取配置文件 -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
  <!--     第一種 *.action    只有訪問到 springmvc/login.action    有.action才觸發
                          springmvc
          第二種 /    符合現在網頁的url 樣式 RESTful風格
          第三種 /*    最好不要使用 最終處理完需要跳轉到一個jsp    代表所有的內容都會經過
          DispatcherServlet    再解析jsp    會報錯
   -->
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
        
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-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 ">

<!-- 配置Controller -->
<bean id="UsersController1" name="/users.action" class="cn.neusoft.controller.UsersController1"></bean>    

<!-- 項目中一般使用 掃描包的方式 進行 配置 -->
<context:component-scan base-package="cn.neusoft.controller.UsersController"></context:component-scan>

<!-- 實際開發中使用 加載註釋的 適配器、映射器    Json轉換器 -->
<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">
    <!-- 配置jsp 的頁面的前綴 -->
    <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框架更具擴展性和靈活性

軟件工程綜合實踐(2)