struts第一个工程,实现传参、跳转

《一》

1.新建工程hellostruts

2.导入struts2-blank.war里的jar包至WebContent的lib下

3.配置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">
  <display-name>hellostrus</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>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>
  
</web-app>

4.写业务处理类He'llAction

package struts.one;

public class HelloAction {
	/**
	 * 方法是public,名字为execute,不能有参数;必须有String返回值,返回值默认为success
	 * @return
	 */
	public String execute() {
		System.out.println("HelloWord--Action");
		return "success";
	}
	//地址栏:项目名称/action的name
}

5.在src右击写struts.xml文件

<?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.devMode" value="true" />
	<!-- 功能模块,一般命名空间与其名字相同,根路径+功能模块与Action匹配 -->
	<package name="default" namespace="/" extends="struts-default">
		<!-- /helloWord method="execute" -->
		<action name="hello" class="struts.one.HelloAction">
			<!--绝对路径,根路径 -->
			<result name="success">/hello.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		
	</package>

	<include file="example.xml" />

	<!-- Add packages here -->
</struts>

6.写index.jsp,hello.jsp

//index.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
hello
<form action="hello" method="post"> 
   <input type="submit" value="登录">
</form>
</body>
</html>

//hello.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
liuxin
</body>
</html>

7.访问http://localhost:8080/hellostrus/hello就OK

《二》

1.写业务处理类LoginAction

package struts.one;

import struts.model.User;

public class LoginAction {
//	// 和表单name一样,叫名字驱动
//	private String username;
//	private String password;
//
//	// 只用到get方法
//	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;
//	}
//
//	public String execute() {// 传数据,IOc控制反转:成员控制----变为action控制
//		// 属性依赖于struts去注入
//		System.out.println("HelloWord--Action");
//		if ("liuxin".equals(username) && "123456".equals(password)) {
//			// username。equals("liuxin")当username为空时没有equals方法,会出现空指针异常
//			return "success";
//		} else
//			return "failture";
//	}

}

2.配置struts.xml

<?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.devMode" value="true" />
	<!-- 功能模块,一般命名空间与其名字相同,根路径+功能模块与Action匹配 -->
	<package name="default" namespace="/" extends="struts-default">
		<!-- /helloWord method="execute" -->
		<action name="hello" class="struts.one.HelloAction">
			<!--绝对路径,根路径 -->
			<result name="success">/hello.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		<action name="dologin" class="struts.one.LoginAction">
			<!--绝对路径,根路径 -->
			<result name="success">/success.jsp</result>
			<result name="failture">/fail.jsp</result>
		</action>
	</package>

	<include file="example.xml" />

	<!-- Add packages here -->
</struts>

3.写login.jsp,

//login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="dologin" method="post">
<h2>请登录</h2>
<br>
    用户名:<input type="text" name="username"><br><br>
    密   码:<input type="password" name="password"><br><br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
   <input type="submit" value="登录">

</form>
</body>
</html>

//success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
成功
</body>
</html>
//fail.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
失败
</body>
</html>

.4.http://localhost:8080/hellostrus/dologin

《三》对象驱动传参

1.LoginAction

public class LoginAction {
	private User user;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String execute() {

		System.out.println("HelloWord--Action");
		if ("liuxin".equals(user.getUsername()) && "123456".equals(user.getPassword())){
			return "success";
		} else
			return "failture";
	}

}

2.写User类

package struts.model;

public class User {
	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;
	}

}

3.login.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="dologin" method="post">
<h2>请登录</h2>
<br>
----------
 <br> 
   对象驱动<br>
    用户名:<input type="text" name="user.getUsername"><br><br>
    密   码:<input type="password" name="user.getPassword"><br><br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
   <input type="submit" value="登录">
  
</form>
</body>
</html>

《四》模型驱动

1.LoginAction

package struts.one;

import com.opensymphony.xwork2.ModelDriven;

import struts.model.User;

public class LoginAction implements ModelDriven<User>{
	private User user=new User();
	

	public String execute() {

		System.out.println("HelloWord--Action");
		if ("liuxin".equals(user.getUsername())&&"123456".equals(user.getPassword())){
			return "success";
		} else
			return "failture";
	}

	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}

}

2.login.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="dologin" method="post">
<h2>请登录</h2>
<br>
----------

   
   模型驱动<br>
    用户名:<input type="text" name="username"><br><br>
    密   码:<input type="password" name="password"><br><br>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
   <input type="submit" value="登录">
</form>
</body>
</html>

3.http://localhost:8080/hellostrus/dologin

猜你喜欢

转载自blog.csdn.net/weixin_42565135/article/details/88559465