Hỏi về Spring MVC
Chào mọi người, hiện tại e vừa học về java Spring MVC. E có tạo 1 project helloworld trong netbeans mà không hiểu sao khi chạy thì nó lên file index.jsp, click vào link trong jsp(/hello) thì nó báo lỗi 404, không tìm được file source. Mọi người giúp e với.
Đây là file web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/*servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Đây là file dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" xmlns:context="http://www.springframework.org/schema/context">
<!--<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>-->
<context:component-scan base-package="controller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
Đây là file HelloController.java thuộc package controller
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView SayHello(){
return new ModelAndView("hello", "message", "Hello Spring 3.0");
}
}
Đây là file index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a href ="hello">click this link</a>
</body>
</html>
còn đây là file hello.jsp của e
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
cấu trúc project của e:
Mới vào e sẽ load file index.jsp, trong trnag đó có 1 link dẫn đến hello.jsp, mà không hiểu sao nó không hiểu (dường như nó không vào file dispatch-servlet.xml
vào /hello.htm nhé, cho đủ 20 ký tự
thêm vào /hello.htm thì nó báo localhost page not found bro ạ :((
Bỏ file index.jsp vào thư mục jsp xem sao
a href của file index.jsp là hello.htm
cho mình hỏi bạn fix đc lối chưa thế , mình cũng bị nhưng không biết fix như nào
bạn vào web.xml xóa cái welcome-file đi vào controller khai báo 1 cái @requestmapping với value là “/” , để cái welcome-file thì phải cấu hình cho nó đúng chứ không là nó sẽ trỏ sai còn muốn cấu hình kiểu welcome-file mình thấy người ta hay sài 1 page redirect xong cũng phải chạy qua controller để chạy đến trang index
bạn cong_5 cho mình ních fb để mình hỏi cái này đc không
Cảm ơn bạn
Lam Ngoc
Lam Ngoc is on Facebook. Join Facebook to connect with Lam Ngoc and others you may know. Facebook gives people the power to share and makes the world more open and connected.
cảm ơn bạn