struts2一个简单实例

1、导入jar包

可以参考struts官方包里的实例,apps目录下有几个war包,解压缩,里面就是一个完整的struts实例,将lib目录下的jar文件复制过来就可以了

2、web.xml

需要在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>

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <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>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

3、struts.xml(配置action)

<?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>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="ru" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>
         <action name="login" class="com.ru.domain.loginaction">
            <result name="sucess">
               /jsp/logsucess.jsp
            </result>
             <result name="error">
               /jsp/logerror.jsp
            </result>
        </action>
    </package>
</struts>

4、LoginAction.java(action类)

package com.ru.action;

import com.opensymphony.xwork2.ActionSupport;
import com.ru.service.UserCheck;

public class LoginAction extends ActionSupport {
	
	private String username;
	private String password;
	
	
	public void setUsername(String username) {
		this.username = username;
	}
	public void setPassword(String password) {
		this.password = password;
	}

		@Override
		public String execute() throws Exception {
		//UserCheck是一个service类
			UserCheck uc=new UserCheck();
			if(uc.result(username, password)=="sucess"){
				return "sucess";
			}else{
				return "error";
			}
			
		}
}




5、login.jsp

注意的是:action实现的是重定向,说以需要完整访问路径,这里使用<base href="<%=basePath%>">,获取完整路径

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib  uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 使用struts标签创建一个表单 -->
	<s:form action="login" method="post">
		<s:textfield key="用户名" name="username"></s:textfield>
		<s:password key="密 码" name="password"></s:password>
		<s:submit value="登录"></s:submit>
	</s:form>
</body>
</html>

猜你喜欢

转载自tydldd.iteye.com/blog/1720153