标签:
struts2相对于struts1来说简单了很多,并且功能强大了很多,我们可以从几个方面来看:
<?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>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>
<%--
Created by IntelliJ IDEA.
User: icarus
Date: 2016/7/2
Time: 12:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<title>登录</title>
</head>
<body>
<form method="post" action="<%=path%>/new/login.action">
用户名:<input type="text" name="userName"><br/>
密码:<input type="password" name="passWord"><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: icarus
Date: 2016/7/2
Time: 12:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
登录成功
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: icarus
Date: 2016/7/2
Time: 12:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录失败</title>
</head>
<body>
登录失败
</body>
</html>
package cn.lovepi.chapter01.action;
/**
* Created by icarus on 2016/7/2.
* 创建表单数据验证类
*/
public class LoginAction {
// 表单属性
private String userName;
private String passWord;
/**
* 执行表单验证操作
* @return 验证状态
*/
public String execute(){
if (userName.equals("wangxin")&&passWord.equals("123456")){
return "success";
}else {
return "fail";
}
}
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;
}
}
<?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>
<!--bean 标签,用于创建一个Java Bean实例-->
<!--constant 标签,用于Struts2默认行为标签-->
<!--package 标签,包标签 用于区分不同的请求文件的标签,比方说网站前台与后台的请求-->
<!--include 标签,用于引入其他的xml配置文件-->
<!--接下来详细介绍constant的主要用法-->
<!--配置web默认编码集,相当于HttpServletRequest.setCharacterEncoding用法
但是这样设置相当于写在了过滤器中,以后所有的访问都不需要在手动指定编码格式-->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!--默认的Struts2的请求后缀是.action 假如我们不配置此项属性的话,Struts2之后拦截
后缀为action的请求,在配置此项之后便会拦截action/do后缀的请求-->
<constant name="struts.action.extension" value="action,do"/>
<!--设置浏览器是否缓存静态内容,默认为true,在我们开发阶段建议关闭,
防止修改后测试不到-->
<constant name="struts.serve.static.browserCache" value="false"/>
<!--struts2配置文件重新修改后,系统是否自动重新加载配置文件,默认为false。
开发阶段建议配置为true,便于开发。-->
<constant name="struts.configuration.xml.reload" value="true"/>
<!--开发模式下使用,可以打印出更加详细的错误信息-->
<constant name="struts.devMode" value="true"/>
<!--默认视图主题-->
<constant name="struts.ui.theme" value="simple"/>
<!--接下来详细介绍package的主要用法-->
<!--name:包名,用于被别的包调用或继承。
namespace:命名空间,选填,url连接必须加入/new/action.***
extends:继承,Struts2中的所有包默认继承自struts-default包-->
<package name="test" namespace="/new" extends="struts-default">
<!--action相当于以前的servlet的概念,对应一个请求的name为请求的url地址。
也就是说我们想要方法我们所配置的action的话,其访问路径为:
localhost:8080/项目名称/包(命名空间)/login.do(按照上面的配置)
-->
<action name="login" class="cn.lovepi.chapter01.action.LoginAction">
<!--配置返回结果对应的跳转页面-->
<result name="success">/chapter01/success.jsp</result>
<result name="fail">/chapter01/fail.jsp</result>
</action>
</package>
</struts>
Struts 2学习(一)Struts 2环境搭建及示例程序编写
标签:
原文地址:http://blog.csdn.net/icarus_wang/article/details/51815273