距离暑假的培训到现在,已有将近三个月。现在回头看看,这三个月的日子过得何止一个“惬意”:每天都是吃吃睡睡,除了体重什么都没增长。某天突然惊醒,才发现按部就班去听课的同学都寥寥无几了。大四了,大家都忙着找工作,考研,好像只有我,全世界只剩我一个人,还在迷茫、彷徨。曾经自以为自己把未来的路规划的挺好而沾沾自喜,现如今,也只剩一声叹息了。
前几天,院里来了企业,进行校园招聘,急急忙忙的跑去。虽然一家外包公司勉强(自我感觉)看中我,但是笔试一结束那叫一个“打击惨重”,曾经还以为自己虽不是精英一枚吧,也算稍有成绩吧!如今再看看,简单的一个例子,&n,神马意思?!抓耳挠腮的那叫一个“惨”字了得!!现在才真正明白“不进步就是在倒退”的深刻含义。眼看着大学时光马上就要消失贻尽,找工作又迫在眉睫,我决定重新拿出笔记,重走我的java之路。
从ssh开始吧。毕业设计还是要用到的!
第一个Struts示例 ---- 用户登录
步骤:
1、下载并导入Struts jar包,放在web-inf/lib下
[img]http://dl.iteye.com/upload/attachment/0077/0485/a4eefda5-75a4-3730-9185-e1341c9de0c5.jpg[/img]
2、配置web.xml文件
3、action 开发
4、配置Struts.xml文件
5、web页面开发
index.jsp
ok.jsp
fail.jsp
只是一个简单的jsp跳转页面
前几天,院里来了企业,进行校园招聘,急急忙忙的跑去。虽然一家外包公司勉强(自我感觉)看中我,但是笔试一结束那叫一个“打击惨重”,曾经还以为自己虽不是精英一枚吧,也算稍有成绩吧!如今再看看,简单的一个例子,&n,神马意思?!抓耳挠腮的那叫一个“惨”字了得!!现在才真正明白“不进步就是在倒退”的深刻含义。眼看着大学时光马上就要消失贻尽,找工作又迫在眉睫,我决定重新拿出笔记,重走我的java之路。
从ssh开始吧。毕业设计还是要用到的!
第一个Struts示例 ---- 用户登录
步骤:
1、下载并导入Struts jar包,放在web-inf/lib下
[img]http://dl.iteye.com/upload/attachment/0077/0485/a4eefda5-75a4-3730-9185-e1341c9de0c5.jpg[/img]
2、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、action 开发
package com.struts2demo.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String userName;
public String myPassword;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMyPassword() {
return myPassword;
}
public void setMyPassword(String myPassword) {
this.myPassword = myPassword;
}
@Override
public String execute() throws Exception {
if ("scott".equals(userName)) {
return SUCCESS;
} else {
return ERROR;
}
}
}
4、配置Struts.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>
<!-- package提供了将多个Action组织为一个模块的方式
package的名字必须是唯一的
package可以扩展
当一个package扩展自另一个package时该package会在本身配置的基础上加入扩展的package的配置
父package必须在子package前配置
name:package名称
extends:继承的父package名称
abstract:设置package的属性为抽象的
抽象的package不能定义action 值true:false
namespace:定义package命名空间
该命名空间影响到url的地址,例如此命名空间为/test那么访问是的地址为http://localhost:8080/struts2/test/XX.action
-->
<!-- struts2的action必须放在一个指定的包空间下定义 -->
<package name="com.struts2demo.action" extends="struts-default">
<!-- 定义处理请求URL为login.action的Action
name 与form表单中调用的action对应
-->
<action name="LoginAction" class="com.struts2demo.action.LoginAction">
<!-- Action配置 一个Action可以被多次映射(只要action配置中的name不同) name:action名称
class: 对应的类的路径
method: 调用Action中的方法名 -->
<!-- 定义处理结果字符串和资源之间的映射关系 -->
<result name="success">ok.jsp</result>
<result name="error">fail.jsp</result>
</action>
</package>
</struts>
5、web页面开发
index.jsp
//一定要在jsp页面头部添加taglib声明
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>
<s:form action="LoginAction"><!-- action 与struts.xml中action标签name一致 -->
<s:textfield name="userName" label="用户名"></s:textfield><br><!-- 使用label,自动对应并添加":"-->
<s:password name="myPassword" label="密 码"></s:password><br>
<s:submit value="登录"></s:submit>
</s:form>
</body>
</html>
ok.jsp
fail.jsp
只是一个简单的jsp跳转页面