本文主要是介绍struts2包及配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
struts2的核心思想
MVC设计模式的实现
是一个控制器
由FilterDispatcher与Action
提供了丰富的视图
JSP
FreeMarker/Velocity
报表支持
AOP面向方面编程
提供大量的Interceptor
支持用户自定义interceptor
1.web.xml中的配置如下:
<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>
2.struts2.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="default" namespace="/" extends="struts-default"><action name="login" class="com.gsww.kingreturns.struts2.excise.LoginAction" method="execute"><result name="success">/welcome.jsp</result><result name="login">/login.jsp</result></action></package></struts>主要属性说明:package-name:用于区别不同的package;必须是唯一的、可用的变量名;用于其它package来继承;package--namespace:用于减少重复代码(和struts1比较);是调用action时输入路径的组成部分;package--extends:用于继承其它package以使用里面的过滤器等东东;action--name:用于在一个package里区别不同的action;必须是唯一的、可用的变量名;是调用action时输入路径的组成部分;action--class:action所在的路径(包名+类名);action--method:action所调用的方法名;
3.编写LoginAction.class
package com.gsww.kingreturns.struts2.excise;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {//该类继承了ActionSupport类。这样就可以直接使用SUCCESS, LOGIN等变量和重写execute等方法private static final long serialVersionUID = 1L;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;}@Overridepublic String execute() throws Exception {if("haha".equals(username) && "hehe".equals(password))//如果登录的用户名=haha并且密码=hehe,就返回SUCCESS;否则,返回LOGINreturn SUCCESS;return LOGIN;}
}
login.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>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"></head><body><s:form action="/login" method="post"><s:label value="系统登陆"></s:label><s:textfield name="username" label="账号" /><s:password name="password" label="密码" /><s:submit value="登录" /></s:form></body>
</html>
welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>My JSP 'welcome.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"></head><body>欢迎${username }!</body>
</html>
这篇关于struts2包及配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!