struts环境搭建以及登陆功能的书写

环境搭建

需要的jar包


web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>strutsForum</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>struts</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

filter包的位置

struts2-core-2.5.16.jar 包下

登陆功能书写

1. UserAction

创建一个UserAction类,继承ActionSupport 再实现ModelDriven<>接口
重写UserAction类中的execute()方法

package com.cky.web;

import com.cky.domain.User;
import com.cky.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{

	public User user=new User();
	
	public String execute() throws Exception {
		
		System.err.println("我已经运行");
		
		UserService userService =new UserService();
		boolean success=userService.findUser(user);
		if(success) {
			return "success";
		}else {
			return "error";
		}
		
		
		
	}

	
	public User getModel() {
		
		
		return user;
	}
	
}

2.struts.xml 配置文件书写

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

	<!--  <package> name:配置包名   extends:固定形式 extends="struts-default"
		   <action> name:浏览器上方访问的url  class:action类路径 method:实现方法
		   <result> name:返回的参数名 
		  
	 -->
	<package name="MyPackage" namespace="/" extends="struts-default">
		<action name="LoginAction" class="com.cky.web.UserAction" method="execute">
			<result name="success">/index.html</result>
			<result name="error">/login.jsp</result>
		</action>
	</package>
</struts>
posted @ 2019-08-11 21:45  丶宇  阅读(114)  评论(0编辑  收藏  举报