struts2的简单流程与xml配置

1.struts2的简单流程

LoginAction.action
package com.wang.firstStrutsTwo;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	
	private String account;
	private String password;
	
	
	
	@Override
	public String execute() throws Exception {
		if("king".equalsIgnoreCase(account) && "123456".equals(password)){
			return SUCCESS;
		}
		return LOGIN;
	}
	
	
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	

}
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="struts" %><!--标签库声明  -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%-- <struts:head theme="ajax"/> --%><!-- 使用ajax主题 -->
<title>My jsp'index.jsp'</title>
</head>
<body>
	<struts:form>
		<struts:label value='登录系统'></struts:label>
		<struts:textfield name='account' label="账号"></struts:textfield>
		<struts:password name='password' label="密码"></struts:password>
		<struts:submit value="登录"></struts:submit>
	
	
	</struts:form>


</body>
</html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="struts" %><!--标签库声明  -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%-- <struts:head theme="ajax"/> --%><!-- 使用ajax主题 -->
<title>My jsp'index.jsp'</title>
</head>
<body>
	欢迎你<struts:property value='account'/><!--  显示action的account属性-->


</body>
</html>
struts.xml
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
        
        <struts>
        	<package name="main" extends="struts-default">
        		<global-results>
        			<result name="login">/login.jsp</result>
        		</global-results>
        		<action name="loginPerson" class="com.wang.firstStrutsTwo.LoginAction">
        			<result name="success">/welcome.jsp</result>
        		</action>
        	</package>
        </struts> 
        
    <!--     一个较为完整的struts.xml配置
    1.constant 配置struts2的属性与struts.properties的作用是一样的
    
    2.package类似于对象,具有可继承的特性, package的name属性指定名称,用于继承时用。
                 如果一个package被另一个继承了,name子package可以引用父package里配置的所有资源,包括interceptor,action,forward
                 
    3.如果配置了namespace属性。如:namespace="namespace1",name访问时需要加上namespace,如:namespace1/person.action             
    
    
    
    
    
    
    
     -->
    <!-- <struts>
    	<constant name="struts.devMode" value="true"></constant>                    开发模式
    	<constant name="struts.action.extension" value="action"></constant>         action后缀
    	
    	<include file="struts-login.xml"></include>									配置文件,可配置多个
    	<include file="struts.xml"></include>
    	<bean class="" name=""></bean>                                              配置bean
    	
    	<package name="main" extends="struts-defaule" namespace="/namespace1">      拦截器配置
    		<interceptors>
    			<interceptor name="loginIntercetor" class="com.king.struts2.interceptor.LoginInterceptor"></interceptor> 配置拦截器
    		
    		</interceptors>
    		
    		<global-results>                                                       配置全局result
    			<result name="login">/login.jsp</result>
    		</global-results>
    		<global-exception-mappings>                                            全局异常配置
    			<exception-mapping result="/exception.jsp" exception="jave.lang.Exception"></exception-mapping>
    		
    		</global-exception-mappings>
    		
    		<action name="loginAction" class="com.king.struts2.action.LoginAction">	配置action
    		<result name="success">/welcome.jsp</result>
    		
    		</action>
    	
    	</package>
    
    
    </struts> -->
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
		<welcome-file-list><!--欢迎文件列表  -->
		<welcome-file>index.jsp</welcome-file><!-- 默认为index.jsp -->
	
	</welcome-file-list>
		<!-- struts2Filter,所有的请求都被映射到struts2上 -->
	<filter>
		<filter-name>struts</filter-name><!--  filter名称-->
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class><!--  filter入口-->
		<init-param>
			<param-name>struts.action.extension</param-name><!--  action后缀参数-->
			<param-value>action</param-value><!--  默认.action-->
		</init-param>
	</filter>
	<!--  struts2的Filter的URL配置-->
	<filter-mapping>
		<filter-name>struts</filter-name><!--filter名称  -->
		<url-pattern>/*</url-pattern><!-- 截获所有URL -->
	
	</filter-mapping>
	
</web-app>
struts2的默认属性位与struts2-core-2.0.11.1.jar包org/apache/struts2下面的default.properties里,用户可以在项目的/WEB-INF/classes下添加struts.properties覆盖默认的配置。常用的需要在struts.properties里重新配置的属性有:

#上传文件的工作目录与文件的最大尺寸
  struts.multipart.saveDir=
  struts.multipart.maxSize=2097152

#struts2的默认后缀
  struts.action.extension=action

#是否是开发模式
  struts.devMode=false

#默认的主题、模板所在文件夹、模板文件后缀
struts.ui.theme=xhtml
struts.ui.templateDir=template
struts.ui.templateSuffix=ftl

#struts2的默认配置文件
  struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值