Struts2基礎學習總結
阿新 • • 發佈:2017-05-14
sub 代碼 resource b- content control button pragma dynamic
Struts2基礎學習總結(一)---入門
Struts 2:
Struts2是一個基於MVC設計模式的Web應用框架,它本質上相當於一個servlet,在MVC設計模式中,Struts2作為控制器(Controller)來建立模型與視圖的數據交互。Struts 2是Struts的下一代產品,是在 struts 1和WebWork的技術基礎上進行了合並的全新的Struts 2框架。其全新的Struts 2的體系結構與Struts 1的體系結構差別巨大。Struts2以WebWork為核心,采用攔截器的機制來處理用戶的請求,這樣的設計也使得業務邏輯控制器能夠與ServletAPI完全脫離開,所以Struts 2可以理解為WebWork的更新產品。雖然從Struts 1到Struts 2有著太大的變化,但是相對於WebWork,Struts 2的變化很小。
Struts2開發環境
1. 拷貝必要jar包
jar包百度雲盤地址:http://pan.baidu.com/s/1skSXbCp
2.在src下建立struts.xml的配置文件
xml文件代碼如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
</struts>
3.在web.xml中配置(增加一個過濾器)
<?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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 增加的過濾器--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
環境配置就完成了接下來測試第一個案列
第一個Struts2案例
1、建立一個login.jsp文件
<%@ 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 XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>login</title> </head> <body id="login"> <form action ="login" method="post"> <p> <label>Username</label> <input class="text-input" type="text" name="username" /> </p> <div class="clear"></div> <p> <label>Password</label> <input class="text-input" type="password" name="password" /> </p> <div class="clear"></div> <p id="remember-password"> <input type="checkbox" name="loginRemember" value="1" /> Remember me </p> <div class="clear"></div> <p> <input class="button" type="submit" value="Sign In" /> </p> </form> </body> </html>
登入頁面
新建一個成功頁面mysuc.jsp
<%@ 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 ‘mysuc.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> 登入成功 <br> </body> </html>
2、controller層代碼
package com.hdj.controller; public class userLogin { private String username; private String password; public String userLogin(){ return "MySuc"; } public String getUsername() { return username; } //采集用戶名信息 public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } //采集密碼 public void setPassword(String password) { this.password = password; } }
3、新增struts.xml文件中配置
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="index"> <result >/index.jsp</result> </action> <action name="login" class="com.hdj.controller.userLogin" method="userLogin" > <result name="MySuc" >/mysuc.jsp </result> </action> </package> </struts>
Tomcat發布並訪問。(http://localhost:8080/day5-14struts2exercise/login.jsp)
點登入跳轉mysuc.jsp頁面
第一個案列到此結束。
Struts2基礎學習總結