struts2 简单应用
web.xml(WEB-INF)
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5 id="WebApp_ID" version="2.5">
6
7 <filter>
8 <filter-name>struts2</filter-name>
9 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
10 </filter>
11
12 <filter-mapping>
13 <filter-name>struts2</filter-name>
14 <url-pattern>/*</url-pattern>
15 </filter-mapping>
16
17
18 </web-app>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5 id="WebApp_ID" version="2.5">
6
7 <filter>
8 <filter-name>struts2</filter-name>
9 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
10 </filter>
11
12 <filter-mapping>
13 <filter-name>struts2</filter-name>
14 <url-pattern>/*</url-pattern>
15 </filter-mapping>
16
17
18 </web-app>
struts.xml(src)
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6 <struts>
7 <package name="mypackage" extends="struts-default">
8
9 <action name="login" class="com.kofzhoubiwen.action.LoginAction">
10 <result name="LOGIN_Y">/WEB-INF/jsp/welcome.jsp</result>
11 <result name="LOGIN_N">/WEB-INF/jsp/login.jsp</result>
12 </action>
13
14 <action name="getweather" class="com.kofzhoubiwen.action.GetWeatherAction" />
15
16 <action name="getuser" class="com.kofzhoubiwen.action.GetUserAction" />
17
18 </package>
19 </struts>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6 <struts>
7 <package name="mypackage" extends="struts-default">
8
9 <action name="login" class="com.kofzhoubiwen.action.LoginAction">
10 <result name="LOGIN_Y">/WEB-INF/jsp/welcome.jsp</result>
11 <result name="LOGIN_N">/WEB-INF/jsp/login.jsp</result>
12 </action>
13
14 <action name="getweather" class="com.kofzhoubiwen.action.GetWeatherAction" />
15
16 <action name="getuser" class="com.kofzhoubiwen.action.GetUserAction" />
17
18 </package>
19 </struts>
LoginAction.java
1 package com.kofzhoubiwen.action;
2
3 import com.kofzhoubiwen.impl.LoginServiceImpl;
4 import com.opensymphony.xwork2.ActionSupport;
5
6 public class LoginAction extends ActionSupport {
7 private String username;
8 private String password;
9 public String getUsername() {
10 return username;
11 }
12 public void setUsername(String username) {
13 this.username = username;
14 }
15 public String getPassword() {
16 return password;
17 }
18 public void setPassword(String password) {
19 this.password = password;
20 }
21 LoginServiceImpl loginserviceimpl=new LoginServiceImpl();
22 @Override
23 public String execute() throws Exception {
24
25 if(loginserviceimpl.login(username, password)){
26 return "LOGIN_Y";
27 }
28 else{
29 return "LOGIN_N";
30 }
31 }
32 }
2
3 import com.kofzhoubiwen.impl.LoginServiceImpl;
4 import com.opensymphony.xwork2.ActionSupport;
5
6 public class LoginAction extends ActionSupport {
7 private String username;
8 private String password;
9 public String getUsername() {
10 return username;
11 }
12 public void setUsername(String username) {
13 this.username = username;
14 }
15 public String getPassword() {
16 return password;
17 }
18 public void setPassword(String password) {
19 this.password = password;
20 }
21 LoginServiceImpl loginserviceimpl=new LoginServiceImpl();
22 @Override
23 public String execute() throws Exception {
24
25 if(loginserviceimpl.login(username, password)){
26 return "LOGIN_Y";
27 }
28 else{
29 return "LOGIN_N";
30 }
31 }
32 }
LoginService.java
1 package com.kofzhoubiwen.servlet;
2
3 public interface LoginService {
4 public Boolean login(String username,String password);
5 }
2
3 public interface LoginService {
4 public Boolean login(String username,String password);
5 }
LoginServiceImpl
1 package com.kofzhoubiwen.impl;
2
3 import com.kofzhoubiwen.servlet.LoginService;
4
5 public class LoginServiceImpl implements LoginService {
6
7 @Override
8 public Boolean login(String username, String password) {
9 if ("admin".equals(username) && "123456".equals(password)) {
10 return true;
11 }
12 else
13 return false;
14 }
15 }
2
3 import com.kofzhoubiwen.servlet.LoginService;
4
5 public class LoginServiceImpl implements LoginService {
6
7 @Override
8 public Boolean login(String username, String password) {
9 if ("admin".equals(username) && "123456".equals(password)) {
10 return true;
11 }
12 else
13 return false;
14 }
15 }
login.jsp(WEB-INF/jsp)
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
2 pageEncoding="ISO-8859-1"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <form action="login.action">
11 user name:<input type="text" name="username" /><br />
12 password:<input type="password" name="password" /><br />
13 <input type="submit" value="submit" /><br />
14 </form>
15 </body>
16 </html>
2 pageEncoding="ISO-8859-1"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <form action="login.action">
11 user name:<input type="text" name="username" /><br />
12 password:<input type="password" name="password" /><br />
13 <input type="submit" value="submit" /><br />
14 </form>
15 </body>
16 </html>