搭建Struts框架

搭建Struts框架

新建项目

搭建Struts框架

【file】-【new】-【web project】

在弹出的对话框中对项目进行命名,点击【finish】

搭建Struts框架

新建项目->
点击项目右键->
MyEclipse->
Add Struts Capabilities选择Struts1.2
->Finish

搭建Struts框架

填写基本的信息,点击【finish】

搭建Struts框架

搭建好之后如上图所示

新建jsp界面

login.jsp界面

<%@ page language="java" 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>login.jsp</title>
</head>
<body>
<form action="<%=basePath%>\loginAction.do">
<table>
<tr>
<td>账号</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登陆"></td>
</tr>
</table>
</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config>
<action-mappings>
<!--type属性:struts请求的具体类,也就是用户定义的servlet
path属性:表单中action里面的值,也就是请求路径
scope属性:请求范围
parameter属性:具体请求的方法名-->
<!-- 控制器的描述 -->
<action path="/loginAction" type="com.itlwc.action.LoginAction"
scope="request">
<!-- 配制跳转页面 -->
<forward name="success" path="/success.jsp"></forward>
<forward name="unsuccess" path="/unsuccess.jsp"></forward>
</action>
</action-mappings>
<!-- 资源文件 -->
<message-resources
parameter="com.itlwc.struts.ApplicationResources" />
</struts-config>

LoginAction.java

创建package命名为com.itlwc.action

在package下面创建文件class文件:LoginAction.java

package com.itlwc.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping; public class LoginAction extends Action {
// ActionMapping对象里面装载的是struts-config.xml文件中的配置信息
// ActionForward类:封装了servlet中的跳转命令
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("lwc".equals(username)&&"123".equals(password))
return mapping.findForward("success");
else
return mapping.findForward("unsuccess");
}
}

success.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>success.jsp</title>
</head>
<body>
登陆成功
</body>
</html>

unsuccess.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>unsuccess.jsp</title>
</head>
<body>
登陆失败
</body>
</html>
上一篇:vim编码相关配置


下一篇:Brian Kernighan 算法