`
midnightvip
  • 浏览: 20372 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

struts2基本配置

阅读更多
1.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>struts2</display-name>
  <!-- struts2的filter,所有的请求都被映射到struts2上  -->
  <filter>
  	<filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!-- Filter名称  根据struts版本不同 -->
  	<init-param>
  		<param-name>struts.action.extension</param-name>
  		<param-value>action</param-value>
  	</init-param>
  </filter>
  <!-- struts2的filter的URL配置  -->
  <filter-mapping>
  	<filter-name>struts</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>


2.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="main" extends="struts-default">
		<global-results>
			<result name="login">/login.jsp</result>
		</global-results>
		<action name="loginPerson" class="com.gjsss.struts2.login.LoginAction">
			<result name="success">/success.jsp</result>
		</action>
	</package>
</struts>

struts.xml放到src根目录下,会自动编辑到web-inf/class里的。
3.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="struts" %>
<%@ taglib uri="/struts-dojo-tags" prefix="dojo" %>
<!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">
<title>My JSP 'index.jsp' starting page</title>
	<dojo:head />
</head>
<body>
	<struts:form action="loginPerson" method="post">
		<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>


  success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-dojo-tags" prefix="dojo" %>
<%@ 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">
<title>My jsp 'index.jsp' starting page</title>
	<dojo:head/>	<!-- 声明使用Ajax主题   -->
</head>
<body>
	登陆成功,欢迎您,<struts:property value="account" />  <!-- 显示Action里的account属性  -->
</body>
</html>

注意:<struts:form>的用法。
4.loginAction.java
package com.gjsss.struts2.login;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private	String account;  //账号
	private String password; //密码
	
	public String execute() {	//主方法
		if("hellonween".equalsIgnoreCase(account) && "1234".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;
	}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics