码迷,mamicode.com
首页 > 其他好文 > 详细

struts2学习的第一个简单例子

时间:2015-02-27 00:13:40      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

下面是本人学习struts2的第一个例子

1,建立工程,把相关的jar包复制到项目的lib目录下面,使用到的jar和公测的大体结构如下图:

技术分享

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>
<!-- 配置欢迎的界面的文件 -->
<welcome-file-list>
<welcome-file>HelloWorld.jsp</welcome-file>
</welcome-file-list>
<!-- 配置filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<!-- 拦截所有的URL用户请求 -->
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

 注意,filter-class最好写新版的filter:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

3,配置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">
<!-- 配置struts2 -->
<struts>
<!-- 配置包,名称为firstStruts -->
<package name="firstStruts" namespace="/" extends="struts-default">
<!-- 配置action -->
<action name="HelloWorld" class="com.xywei.struts2.HelloWorld">
<!-- 配置返回结果 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>

</struts>

4,编写action,在项目的src下面的com.xywei.struts2包下面建立HelloWorld.java类

package com.xywei.struts2;

public class HelloWorld {
private String name;
private String password;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
if(getName().equals("xywei") && getPassword().equals("123")){
return "success";
}else{
return "error";
}

}

}

5,编写jsp文件

HelloWorld.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 ‘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>
<form action="HelloWorld" method="post">
username:<input type="text" name="name"><br> password:<input
type="password" name="password"><br> <input
type="submit" value="submit">
</form>
</body>
</html>

success.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>登录成功</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>
欢迎!登录成功!<%
out.print(request.getParameter("name"));
%>


</body>
</html>

 

error.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>登录失败</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>
登录失败!
<a href="HelloWorld.jsp">返回</a>
</body>
</html>

注意,最为细节的是HelloWorld.jsp页面内的name,password必须和HelloWorld.java文件的一样,否则会报错。

完成之后,部署访问即可以完成第一个struts2例子。

struts2学习的第一个简单例子

标签:

原文地址:http://www.cnblogs.com/listentothecloud20150215/p/4302409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!