上傳jar包,復制jar包到WebRoot/WEB-INF/lib下
jar包 |
作用 |
commons-fileupload-1.2.2.jar |
文件上傳 |
commons-io-2.0.1.jar |
fileupload依賴于io |
commons-logging-1.1.1.jar |
關于日志處理的 |
ognl-3.0.5.jar |
ognl表達式 |
struts2-core-2.3.4.jar |
struts2框架核心庫包 |
xwork-core-2.3.4.jar |
Xwork的核心類庫 |
freemarker-2.3.19.jar |
模板引擎 |
javassist-3.11.0.GA.jar |
編輯Java字節(jié)碼的類庫 |
commons-lang3-3.1.jar |
用來處理Java基本對象方法的工具類包 |
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>
在src下新建一個xml文件struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.sshlearn.action.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
src下新建一個package com.sshlearn.action,并新建class loginAction
package com.sshlearn.action;
public class LoginAction {
private String username;
private String password;
public String execute() {
System.out.println(username+"==="+password);
return "success";
}
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;
}
}
新建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 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 name="login" action="login" method="post">
<input type="text" name="username"/>
<br/>
<input type="text" name="password"/>
<br/>
<input type="submit" value="保存" />
</form>
</body>
</html>
新建result.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 'result.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>
username:${requestScope.username} <br/>
password:${requestScope.password}
</body>
</html>
項目目錄結構如圖
訪問地址localhost:8080/sshlearn/login.jsp池颈,頁面如下
跳轉到result.jsp頁面,結果如下
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者