1_Struts2入门与配置

已登陆为例构造我们第一个struts2程序
导入struts2所必须的jar包,我已经打包上传了
首先不要忘记再web.xml中配置必须的过滤器

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

请求页login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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</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="login.action" method="post">
username:<input type="text" name="username"><br/>
password:<input type="password" name="password"><br/>

<input type="submit" value="submit">

</form>
</body>
</html>

结果页result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 }<br/>
</body>
</html>



业务处理的javabean

package com.test.action;

public class LoginAction {
private String username;
private String password;

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;
}

public String execute() throws Exception{
return "success";
}
}


流程处理的配置页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">
<!-- name对应表单的action,class对应于处理类 -->
<action name="login" class="com.test.action.LoginAction">
<!-- result标签默认匹配success,这里可以省略name="success" -->
<result name="success">/result.jsp</result>
</action>

</package>

</struts>

程序的执行流程:
login.jsp中的action通过struts.xml找到LoginAction的execute方法进行处理,通过方法的返回字符串,找到对应的返回页面。

下面我们采用struts标签并加入验证来重新完成上面的过程
login2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>login2</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="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>

<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>


LoginAction.java

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
private String username;
private String password;

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;
}
//业务方法
public String execute() throws Exception{
if("hello".equals(this.getUsername().trim()) && this.getPassword().trim().equals("world")){
this.username="hello1";
return "success";
}else{
//错误信息将在用户名上面显示
this.addFieldError("username", "username or password error!");
return "failer";
}
}
//表单验证方法
public void validate() {
if(null == this.getUsername() || "".equals(this.getUsername().trim())){
//这里可以使用国际化
this.addFieldError("username", "username required");
}
if(null == this.getPassword() || "".equals(this.getPassword().trim())){
//这里可以使用国际化
this.addFieldError("password", "password required");
}

}

}


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 }<br/>
<%
//这里我要说明的是上面的el表达式与下面的完全不同
//等同于request.getAttribute("username")
//因为下面的语句得到的是真实的入参,而实际的后台属性是存在request中的
//如果后台变更了参数,取到的就不正确了,这点应该十分注意
//request.getParameter("username")
%>
</body>
</html>

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">
<!-- name对应表单的action,class对应于处理类 -->
<action name="login" class="com.test.action.LoginAction">
<!-- result标签默认匹配success,这里可以省略name="success" -->
<result name="success">/result.jsp</result>
<!-- 未通过validate方法的时候跳转到此 -->
<result name="input">/login2.jsp</result>
<result name="failer">/login2.jsp</result>
</action>

</package>

</struts>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值