Spring MVC : 고급 페이지에 대한 기본 컨트롤러를 만드는 방법은 무엇입니까?
나는 표준 spring mvc hello world 애플리케이션 중 하나를 시도하고 컨트롤러를 루트에 매핑하고 싶습니다. (예 : http://numberformat.wordpress.com/2009/09/02/hello-world-spring-mvc-with-annotations/ ) 실제 차이점은 host \ appname \ something에 매핑하고 host \ appname에 매핑하고 싶습니다.
내 index.jsp를 src \ main \ webapp \ jsp에 배치하고 web.xml에 환영 파일로 매핑했습니다. 나는 시도했다 :
@Controller("loginController")
public class LoginController{
@RequestMapping("/")
public String homepage2(ModelMap model, HttpServletRequest request, HttpServletResponse response){
System.out.println("blablabla2");
model.addAttribute("sigh", "lesigh");
return "index";
}
내 컨트롤러로 내 바람둥이 콘솔에 아무것도 적은데. 내가 어디에서 엉망인지 아는 사람이 있습니까?
내 web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- Index -->
<welcome-file-list>
<welcome-file>/jsp/index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
mvc-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="de.claude.test.*" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Spring 3.0.5.release를 사용하고 있습니다.
아니면 이것이 불가능하고 내 index.jsp를 web-inf의 루트에 다시 포함가 내 jsp 내부 어딘가에 리디렉션을 컨트롤러가 선택해야할까요?
Sinhue의 설정을 사용한 후에도 같은 문제가 있었지만 해결했습니다 .
문제는 WebContent 디렉토리에 index.jsp 파일이있을 수 있습니다. (Tomcat?)가 "/"에서 "/index.jsp"로 전달되고 될 것입니다. 그것을 거부했을 때 요청이 더 이상 전달되지 않았습니다.
문제를 진단하기 위해 한 번의 작업을 수행하여 콘솔에 인쇄하는 것이 었습니다. 이 내가 만드는 요청이 http : // localhost / myapp / 에 대한 것이지만 서블릿 경로가 "/index.html"로 변경되고 있음을 보여줍니다 . 나는 그것이 "/"일이 예상하고 있었다.
@RequestMapping("*")
public String hello(HttpServletRequest request) {
System.out.println(request.getServletPath());
return "hello";
}
요약하면 따라야 할 수 있습니다.
- 서블릿 매핑 사용
<url-pattern>/</url-pattern>
- 컨트롤러 사용
RequestMapping("/")
- web.xml에서 welcome-file-list 제거
- WebContent에 기본 페이지로 제공되는 파일 (index.html, index.jsp, default.html 등)이 없습니다.
도움이 되셨기를 바랍니다.
리디렉션은 하나의 옵션입니다. 시도해 볼 수있는 한 유전 WAR의 루트에 배치하는 매우 간단한 색인 페이지를 만드는 것입니다.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:redirect url="/welcome.html"/>
그런 다음 해당 URL로 컨트롤러를 다음과 같이 매핑합니다.
@Controller("loginController")
@RequestMapping(value = "/welcome.html")
public class LoginController{
...
}
마지막으로 web.xml에서 (새) 강화 JSP에 액세스 할 수있는 다음을 선언합니다.
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
기본 뷰에 대한 컨트롤러 방법을 간단히 매핑 할 수 있습니다. 예를 들어 기본 페이지로 index.html이 있습니다.
@RequestMapping(value = "/", method = GET)
public String index() {
return "index";
}
완료되면 기본 애플리케이션 시나리오 페이지에 액세스 할 수 있습니다.
E.g http://localhost:8080/myapp
나를 위해 작동하지만 몇 가지 차이점이 있습니다.
- web.xml에 welcome-file-list가 없습니다.
- 클래스 수준에서 @RequestMapping이 없습니다.
- 메서드 수준에서는 @RequestMapping ( "/")
나는 이것이 큰 차이가 아니라는 것을 알고 있지만 이것이 내 구성이며 Spring MVC 3.0.5에서 작동한다는 것을 확신합니다 (지금은 일하고 있지 않습니다).
하나 더. web.xml에 디스패처 구성을 표시하지 않지만 접두사가있을 수 있습니다. 다음과 같아야합니다.
<servlet-mapping>
<servlet-name>myServletName</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
이것이 귀하의 경우가 아니라면 url-rewrite 필터가 필요하거나 리디렉션 솔루션을 시도하십시오.
편집 : 귀하의 질문에 대답하면 내 뷰 리졸버 구성도 약간 다릅니다.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
더 간단한 방법으로 해결할 수 있습니다 : web.xml에서
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
그런 다음 @RequestMapping ( "index.htm")을 사용하여 index.htm을 처리하려는 컨트롤러를 사용합니다. 또는 인덱스 컨트롤러를 사용하십시오.
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
<bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</bean>
스프링 xml 파일에 항목을 하나 더 넣으십시오.mvc-dispatcher-servlet.xml
<mvc:view-controller path="/" view-name="index"/>
이것을 XML에 넣은 후 파일에서 언급했듯이 사용자 정의 JSP 폴더에 기본보기 또는 jsp 파일을 넣으십시오 mvc-dispatcher-servlet.xml
.
index
jsp 이름으로 변경 하십시오.
이를 달성하는 한 가지 방법은 환영 파일을 파일의 컨트롤러 요청 경로에 매핑하는 것입니다 web.xml
.
[web.xml]
<web-app ...
<!-- Index -->
<welcome-file-list>
<welcome-file>home</welcome-file>
</welcome-file-list>
</web-app>
[LoginController.java]
@Controller("loginController")
public class LoginController{
@RequestMapping("/home")
public String homepage2(ModelMap model, HttpServletRequest request, HttpServletResponse response){
System.out.println("blablabla2");
model.addAttribute("sigh", "lesigh");
return "index";
}
SpringMVC 웹앱에서 사용하는 솔루션 DefaultController
은 다음과 같은 간단한 클래스 를 만드는 것입니다 .-
@Controller
public class DefaultController {
private final String redirect;
public DefaultController(String redirect) {
this.redirect = redirect;
}
@RequestMapping(value = "/")
public ModelAndView redirectToMainPage() {
return new ModelAndView("redirect:/" + redirect);
}
}
리디렉션은 다음 스프링 구성을 사용하여 삽입 할 수 있습니다.-
<bean class="com.adoreboard.farfisa.controller.DefaultController">
<constructor-arg name="redirect" value="${default.redirect:loginController}"/>
</bean>
는 ${default.redirect:loginController}
디폴트로한다 loginController
그러나 삽입에 의해 변경 될 수있는 default.redirect=something_else
등 / 스프링 특성 파일에 환경 변수 설정
@Mike가 위에서 언급했듯이 나는 또한 :-
- 파일
<welcome-file-list> ... </welcome-file-list>
에서 섹션을 제거했습니다web.xml
. - 기본 페이지로 간주 될 수있는 WebContent에 앉아있는 모든 파일이없는 (
index.html
,index.jsp
,default.html
, 등)
이 솔루션은 Spring이 당신이 좋아할 수도 있고 아닐 수도있는 리디렉션에 대해 더 많이 걱정할 수있게합니다.
'ProgramingTip' 카테고리의 다른 글
data.frame 또는 대규모 행 행 (0) | 2020.12.04 |
---|---|
자바 펼쳐에서 클래스 클래스 속성을 추가 할 수없는 이유는 무엇입니까? (0) | 2020.12.04 |
jquery의 데이터 속성에서 부울 데이터 검색 (0) | 2020.12.04 |
$ HOME과 '~'(물결표)의 차이점은 무엇입니까? (0) | 2020.12.04 |
dis.dis의 출력을 어떻게 이해해야합니까? (0) | 2020.12.04 |