在struts2框架中使用servlet做接口

在struts2框架中, 由于在web.xml中配置了struts2 的核心控制器,再配置servlet接口的话, 会被struts优先拦截到.从而出现servlet 404的情况

	
	
		struts2
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	
	
		struts2
		/*
	



解决办法 : 

在web.xml中配置.servlet

	
	
	
		DemoServlet
		cn.yuge.servlet.DemoServlet
	
	
		DemoServlet
		/demoservlet.servlet
		
	


这样配置之后, 再访问servlet, 就可以通过struts的拦截了

测试demo:

1. 将web项目部署到tomcat ; 

2. 访问index.jsp 输入, 并提交

3. action 打印正常

4. 浏览器输入 http://localhost:8080/struts2-servlet/demoservlet.servlet?username=123, 后台成功打印 username的参数. 

5. 证明已成功通过struts拦截


web.xml



	struts2-servlet

	
	
		struts2
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	
	
		struts2
		/*
	

	
	
	
		DemoServlet
		cn.yuge.servlet.DemoServlet
	
	
		DemoServlet
		/demoservlet.servlet
		
	

	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	


struts.xml





	
		
			/success.jsp
		
	



index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




Insert title here


	
用户名:
年龄:



DemoAction

package cn.yuge.action;

import cn.yuge.domain.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class DemoAction extends ActionSupport implements ModelDriven {

	private User user = new User();

	@Override
	public User getModel() {
		return user;
	}

	public String demo() {
		System.out.println(user);
		return SUCCESS;
	}

}
User

package cn.yuge.domain;

public class User {

	private Long id;
	private String name;
	private String age;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

}


success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




Insert title here

执行成功




DemoServlet

package cn.yuge.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DemoServlet extends HttpServlet {

	private static final long serialVersionUID = 6792396567928634227L;// 版本信息

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		response.setContentType("text/html;charset=utf-8");// 设置响应编码
		// 浏览器输入
		// http://localhost:8080/struts2-servlet/demoservlet.servlet?username=123
		String username = request.getParameter("username");

		response.getWriter().write(username);

	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		doPost(request, response);
		// response.getWriter().write("-1");
	}
}


 
 

你可能感兴趣的:(框架,:,Struts2)